zoukankan      html  css  js  c++  java
  • C#连接SQLite的字符串

    一、C#在不同情况下连接SQLite字符串格式

    1、Basic(基本的)

    Data Source=filename;Version=3;

    2、Using UTF16(使用UTF16编码)

    Data Source=filename;Version=3;UseUTF16Encoding=True;

    3、With password(带密码的)

    Data Source=filename;Version=3;Password=myPassword;

    4、Using the pre 3.3x database format(使用3.3x前数据库格式)

    Data Source=filename;Version=3;Legacy Format=True;

    5、Read only connection(只读连接)

    Data Source=filename;Version=3;Read Only=True;

    6、With connection pooling(设置连接池)

    Data Source=filename;Version=3;Pooling=False;Max Pool Size=100;

    7、Using DateTime.Ticks as datetime format()

    Data Source=filename;Version=3;DateTimeFormat=Ticks;

    8、Store GUID as text(把Guid作为文本存储,默认是Binary)

    Data Source=filename;Version=3;BinaryGUID=False;

    如果把Guid作为文本存储需要更多的存储空间

    9、Specify cache size(指定Cache大小)

    Data Source=filename;Version=3;Cache Size=2000;

    Cache Size 单位是字节

    10、Specify page size(指定页大小)

    Data Source=filename;Version=3;Page Size=1024;

    Page Size 单位是字节

    11、Disable enlistment in distributed transactions

    Data Source=filename;Version=3;Enlist=N;

    12、Disable create database behaviour(禁用创建数据库行为)

    Data Source=filename;Version=3;FailIfMissing=True;

    默认情况下,如果数据库文件不存在,会自动创建一个新的,使用这个参数,将不会创建,而是抛出异常信息

    13、Limit the size of database(限制数据库大小)

    Data Source=filename;Version=3;Max Page Count=5000;

    The Max Page Count is measured in pages. This parameter limits the maximum number of pages of the database.

    14、Disable the Journal File (禁用日志回滚)

    Data Source=filename;Version=3;Journal Mode=Off;

    This one disables the rollback journal entirely.

    15、Persist the Journal File(持久)

    Data Source=filename;Version=3;Journal Mode=Persist;

    二、C#连接SQLite数据库连接字符串说明

    基本连接Sqlite数据库:

    Data Source=mydb.db;Version=3;

    --"Version" 的可能值: "2″ 指 SQLite 2.x (default);"3″ 指 SQLite 3.x

    连接同时创建一个新的Sqlite数据库:

    Data Source=mydb.db;Version=3;New=True;
    启用压缩连接Sqlite数据库:
    Data Source=mydb.db;Version=3;Compress=True;
    指定连接Sqlite数据库的缓存大小:
    Data Source=mydb.db;Version=3;Cache Size=3000;

     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。

      下载地址:  http://sqlite.phxsoftware.com/

      添加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]);
                                }
                            }
                        }

     

     2.3 使用原生态的ADO.NET访问SQLite

      原生态的访问,就是说直接用connection和command这些对象打开数据库,然后打开连接,进行数据的操作。

    using (DbConnection conn = new SQLiteConnection( 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]);
                                }
                            }
                        }

     


  • 相关阅读:
    Protected和Default的区别
    将数组中负数放在正数前面
    java.io包和杯子测楼
    hadoop基础
    极限编程和JUnit
    接口和抽象类
    C# 中窗口AutoScaleMode属性
    计算机的自启动管理
    labview中的移位寄存器、循环隧道,自动索引隧道的区别
    发现C#winform编程中不常用的控件(一)<FlowLayoutPanel控件><拆分器控件Splitcontainer >
  • 原文地址:https://www.cnblogs.com/CSharpLover/p/5193700.html
Copyright © 2011-2022 走看看