一、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, ");
                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[]);
                            }
                        }
                    }

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

C#连接SQLite的字符串的更多相关文章

  1. C#.net连接SQLite及遇到的问题

    1.Slite简介 SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需 ...

  2. 一步一步教你用c# entity framework6 连接 sqlite 实现增删改查

    使用entity framework6 连接 SQLite 数据库 前言 很多小型应用程序中,都要使用数据库,而现在比较流行的本地数据库非SQLite莫属. 第一步:前期准备 开发环境:vs2015 ...

  3. 一起学微软Power BI系列-使用技巧(6) 连接Sqlite数据库

    好久没有研究Power BI了,看到高飞大神弄的东西,太惭愧了.今天有个小东西,数据在Sqlite里面,想倒腾到Power BI Desktop里面折腾一下,结果发现还不直接支持.所以只好硬着头皮上去 ...

  4. C#连接Sqlite 出现:混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。的解决方案

    C#连接Sqlite 出现: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集.的解决方案 C#连接sqlite数据库代码 ...

  5. Delphi 2010下使用sqlitesimpledelphi连接SQLite数据库及中文乱码问题的解决

    应女朋友的要求,要写一款销售管理的软件.用于管理服装店每天的销售记录,已及管理服装店的客户,并对客户进行生日提醒 因为之前使用C#写过一款家庭管理软件,主要是自己用,所以使用了服务器型数据库MySQL ...

  6. Sqlite 常用操作及使用EF连接Sqlite

    Sqlite是一个很轻,很好用的数据库.兼容性很强,由于是一款本地文件数据库,不需要安装任何数据库服务,只需引入第三方开发包就可以.Sqlite的处理速度比MySql和PostgreSQL更快,性能很 ...

  7. Qt——信号槽连接:基于字符串与基于函数的连接之间的不同

    从Qt5.0开始,Qt提供了两种不同的方式进行信号槽的连接:基于 字符串 的连接语法.基于 函数 的连接语法.这两种语法各有利弊,下面对它们的不同点进行总结. 以下几部分详细解释了它们之间的不同,并说 ...

  8. VS2010连接SQLite数据库

    Visual studio 2010及以上版本,连接SQLite数据库 1.在Sqlite开发站点下载SQLite的.exe安装包 Ctrl+F搜索这条语句:This is the only setu ...

  9. Java 连接SQLite数据库

    下载jar包: http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz public class TestSQLite { ...

随机推荐

  1. CICS日志---内存问题

    Level 9 COCITOOL_XA: Connected! [2014-01-09 19:46:24.296834][22347888] Level 0 TestPerormence: GDAO ...

  2. java基础-基础类型包装类型

    想要对基本类型数据进行更多的操作,最方便的方式就是将其封装成对象. 为啥呢?因为在对象描述中就可以定义更多的属性和行为对该基本数据类型进行操作. [八种基本数据类型的包装类] byte --Byte ...

  3. JAVA_SE复习(异常)

    异常.调试和断言 一. 异常的分类 1. 可查异常    例: 2. 不可查异常  例:Runtime Exception 3. 异常的分类结构: 1. 不执行finally 子句的唯一情况是虚拟机关 ...

  4. Java抓取网页数据

    http://ayang1588.github.io/blog/2013/04/08/catchdata/ 最近处于离职状态,正赶清闲,开始着手自己的毕业设计,课题定的是JavaWeb购物平台,打算用 ...

  5. php框架-yii

    安装 修改权限问题:runtime;web/assets(mac上) 配置cookie加密串 config/web.php 修改cookieValidationKey L12 配置数据库 控制器: 默 ...

  6. canvas 的学习

    canvas 绘制直线的API有: 1.moveTo()起点坐标. 2.lineTo()绘制的直线 3. fillStyle以及 flii()是绘制实体的 4. strokeStyle 和stroke ...

  7. 完美解决IE6中fixed抖动问题的方法

    我们可以通过position:fixed来实现元素的固定效果,如网页中底部的"回到顶部菜单",底部的toolbar,对联广告等等,可惜fixed属性在IE6及以下是不支持的.通常的 ...

  8. 就谈个py 的装饰器 decorator

    很早很早就知道有这么个 装饰器的东西,叫的非常神秘. 包括c#  和 java 中都有这个东西, c#中叫做attribut 特性,java中叫做Annotation 注解,在偷偷学习c#教程的时候, ...

  9. C++ 容器及选用总结

    目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...

  10. 转 在无法通过yum下载非标准包时,怎么办

    在CentOS下,我们可以通过yum来下载或更新rpm包,但是标准的源(repository)里只提供一部分的rpm包,虽然大部分情况下,这些包是够用的.但是有时候还是需要下载其他的一些非标准的包,如 ...