C#连接SQLite数据库方法总结01

1. SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月. 至今已经有10个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

  2 C#连接,操作SQLite数据库

  2.1 结合Enterprise Library连接,操作SQLite

  企业库是我们常用的框架之一,可以从http://entlib.codeplex.com/下载。安装之后有源代码和chm的文档。最新版本目前是V5.0。里面的很多思想更值得我们程序员去研究,例如:如何设计可扩展的组建?

  企业库中的数据访问组件更是我们常用的数据访问组件之一。组件默认支持SQL Server和Oracle的数据库访问,支持自定义的扩展。

  使用企业库操作SQLite数据库,需要用到企业库的一个扩展组件,Enterprise Library Contrib 。里面扩展了企业库的很多功能。其中对数据库的扩展包括了访问操作SQLite,让我们可以像在操作SQL SERVER那样,保持代码不用很大的修改,可以很容易的过渡到SQLite上。

  遗憾的是目前的这个entlib contrib的版本是V4.1,它只支持企业库的V4.1版本,也就是说它只能和V4.1版本的企业库的数据访问组件配合使用。否则会报错。

  在http://entlib.codeplex.com/上也可以下载到历史版本,也就是可以下载到V4.1。

  用法也可以参考:ASP.NET: Using SQLite with Enterprise Library 3.1

  首先在web.config或者是app.config中添加如下配置

< configuration >
  
< configSections >
    
< section name = " dataConfiguration " type = " Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null />
   </ configSections >
  
< dataConfiguration defaultDatabase = "

" >
     < providerMappings >
      
< add databaseType = " EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SqLite, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null "
        name
= " System.Data.SQLite " />
    
</ providerMappings >
  
</ dataConfiguration >
  
< connectionStrings >
    
< add name = " sqlite " connectionString = " Data Source=|DataDirectory|\db;Pooling=true;FailIfMissing=false "
      providerName
= " System.Data.SQLite " />
  
</ connectionStrings >
</ configuration >

 

  上面的connectionstring配置节的db就是SQLite的数据库文件,将它放在Web应用的App_Data目录,|DataDirectory|就代表这个目录的位置,后面的就是文件名。

  剩下的就是我们使用企业库访问SQL Server是一样的了。

Database db = DatabaseFactory.CreateDatabase ( " ConnectionString " );
                DbCommand comm
= db.GetStoredProcCommand( " GetUserByID " );
                IDataReader reader
= null ;
                db.AddInParameter(comm,
" UserID " , DbType.String, " 12 " );
                using (reader
= db.ExecuteReader(comm))
                {
                  
                }

 

  2.2 使用SQLite.NET访问SQLite

  SQLite.NET也是一个数据访问组件,其中的System.Data.SQLite 就好像是.NET自带的System.Data.SqlClient一样。里面包含了connection、command等数据访问的常用对象,只是他们前面都有一个前缀sqlite。

  添加System.Data.SQLite 的引用之后。在配置文件(web.config or app.config)中添加如下配置

< system.data >
    
< DbProviderFactories >
      
< remove invariant = " System.Data.SQLite " />
      
< add name = " SQLite Data Provider " invariant = " System.Data.SQLite " description = " .Net Framework Data Provider for SQLite " type = " System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139 " />
    
</ DbProviderFactories >
  
</ system.data >

 

  也就是添加一个DbProviderFactory的创建源,在代码中就可以使用DbProviderFactory类来创建SQLite的数据访问对象了。

DbProviderFactory fact = DbProviderFactories.GetFactory( " System.Data.SQLite " );
                    using (DbConnection conn
= fact.CreateConnection())
                    {
                        conn.ConnectionString
= System.Configuration.ConfigurationManager.ConnectionStrings[ " sqlite " ].ConnectionString;
                        conn.Open();
                        DbCommand comm
= conn.CreateCommand();
                        comm.CommandText
= " select * from customer " ;
                        comm.CommandType
= CommandType.Text;
                        using (IDataReader reader
= comm.ExecuteReader())
                        {
                            
while (reader.Read())
                            {
                                Response.Write(reader[
0 ]);
                            }
                        }
                    }

 

转载于:https://www.cnblogs.com/bihailantian/archive/2010/10/01/1840685.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值