C# 程序中使用 SQLite 数据库


差不多在 3 年前我就关注过 SQLite,也整理了一篇 SQLite 的介绍:​ ​开源嵌入式数据库 SQLite 简介​ ​, 还在我的一个查手机归属地软件中使用了 SQLite 数据库,​ 现在又要用到 C# 了,所以决意了解下 C# 中如何使用 SQLite,之所以选择 SQLite 作为自己的文件型数据,主要是看中它的非常 小型 单文件 嵌入型 ,更支持 多进程 访问。

SQLite 官方网站:​ ​http://www.sqlite.org​ ​,你可以在那里下载到一个 sqlite3.exe,用命令行模式来管理你的数据库文件。或用其他比较好的管理工具:​ ​sqliteman​ ​(也支持 UTF-8) 、​ ​sqlitebrowser​ ​ 和 ​ ​SQLite Administrator​ ​。我比较喜欢 sqlitebrowser,但更推荐可设置字符集的 ​ ​SQLite Expert​ ​(可下载到免费的个人版)。

既然是嵌入式数据库,只要些动态库就行,如 dll 原生库,jar 包或其他语言的组件库形式,.NET 中也不例如,要用到 ​ ​System.Data.SQLite​ ​,下载安装后在安装目录中你有你所需的各种文件,使用 SQLite 只要在你的 .NET 应用引入 System.Data.SQLite.dll,如果要支持 LINQ,还要引入 System.Data.SQLite.Linq.dll。它支持到 ADO.NET 3.5,被 NHibernate 支持,并能集成在 VS2005/2008,且提供 64 位版,WinCE 版的动态库。SQLite.NET 也带了个帮助文档。

下面来直接看个例子,基本是借用了 ​ ​C#使用SQLite数据库(asp.net/winform)​ ​ 中的例子。




01


02


03


04


05


06


07


08


09


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38


39


40


41


42


43


44


45


46


47


48


49


50


51


52


53


54


55


56


57


58


59


60


61




​using​ ​ ​ ​System; ​


​using​ ​ ​ ​System.Data; ​


​using​ ​ ​ ​System.Data.SQLite; ​



​//C# 使用 SQLite 数据测试程序 ​


​public​ ​ ​ ​class​ ​ ​ ​Program ​


​{ ​


​public​ ​ ​ ​static​ ​ ​ ​void​ ​ ​ ​Main(​ ​​ ​string​ ​​ ​[] args) ​


​{ ​


​using​ ​ ​ ​(SQLiteConnection con = ​ ​​ ​new​ ​ ​ ​SQLiteConnection(​ ​​ ​"Data Source=c:\\test.db3;Pooling=true;FailIfMissing=false"​ ​​ ​)) ​


​{ ​


​//打开数据库文件 c:\\test.db3,不存在则创建 ​


​con.Open(); ​



​using​ ​ ​ ​(SQLiteCommand cmd = ​ ​​ ​new​ ​ ​ ​SQLiteCommand()) ​


​{ ​


​cmd.Connection = con; ​



​//检查是否存在表 test,不存在则创建 ​


​Boolean testTableExists = ​ ​​ ​false​ ​​ ​; ​


​cmd.CommandText = ​ ​​ ​"SELECT * FROM sqlite_master WHERE type='table' and name='test'"​ ​​ ​; ​


​using​ ​​ ​(SQLiteDataReader dr = cmd.ExecuteReader()) ​


​{ ​


​if​ ​ ​ ​(dr.Read()) ​


​{ ​


​testTableExists = ​ ​​ ​true​ ​​ ​; ​


​} ​


​} ​


​if​ ​ ​ ​(!testTableExists) ​


​{ ​


​cmd.CommandText = ​ ​​ ​"CREATE TABLE [test] (id int, name nvarchar(20))"​ ​​ ​; ​


​cmd.ExecuteNonQuery(); ​


​} ​



​//清空 test 表 ​


​cmd.CommandText = ​ ​​ ​"DELETE FROM [test]"​ ​​ ​; ​


​cmd.ExecuteNonQuery(); ​



​//插入测试数据 ​


​for​ ​ ​ ​(​ ​​ ​int​ ​ ​ ​i = 1; i <= 5; i++) ​


​{ ​


​cmd.CommandText = ​ ​​ ​string​ ​​ ​.Format(​ ​​ ​"INSERT INTO [test] VALUES ({0}, '中文测试')"​ ​​ ​, i); ​


​cmd.ExecuteNonQuery(); ​


​} ​



​//读取数据 ​


​cmd.CommandText = ​ ​​ ​"SELECT * FROM [test]"​ ​​ ​; ​


​using​ ​ ​ ​(SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) ​


​{ ​


​while​ ​ ​ ​(dr.Read()) ​


​{ ​


​Console.WriteLine(​ ​​ ​"第{0} 条:{1}"​ ​​ ​, dr.GetValue(0), dr.GetString(1)); ​


​} ​


​} ​


​} ​


​} ​



​Console.WriteLine(​ ​​ ​"Press any key to continue..."​ ​​ ​); ​


​Console.ReadKey(); ​


​} ​


​}​



运行之后,你会发现,中文可以插入,并且显示出来也没问题,但是用一般的 SQLite 管理工具看到的却是乱码。同样,在其他客户端中输入的中文让上面程序读出来也是乱码。幸运的是还是找到一种客户端工具 ​ ​SQLite Expert​ ​(可下载到免费的个人版), 看到的结果是中文,这是因为 System.Data.SQLite 默认使用的是 UTF-8 字符集,而 SQLite Expert 有字符集的选择可设置的。其他的客户端默认的字符集都是 ANSI,也就是 ISO8859-1 吧。

其他相关内容:连接字符串的参数配置:

在安装后的 SQLite.NET Help 帮助文档中可以查到详细的连接参数说明. 查 ConnectionString Property,或看链接:​ ​Connection String​ ​:

Parameter

Values

Required

Default

Version

3

N

3

Data Source

{filename}

Y

Version

3

N

3

UseUTF16Encoding

True

False

N

False

DateTimeFormat

Ticks - Use DateTime.Ticks

ISO8601 - Use ISO8601 DateTime format

N

ISO8601

BinaryGUID

Yes/On/1 - Store GUID columns in binary form

No/Off/0 - Store GUID columns as text

N

On

Cache Size

{size in bytes}

N

2000

Synchronous

Normal - Normal file flushing behavior

Full - Full flushing after all writes

Off - Underlying OS flushes I/O's

N

Normal

Page Size

{size in bytes}

N

1024

Password

{password}

N

Enlist

Y - Automatically enlist in distributed transactions

N - No automatic enlistment

N

Y

Pooling

True - Use connection pooling

False - Do not use connection pooling

N

False

FailIfMissing

True - Don't create the database if it does not exist, throw an error instead

False - Automatically create the database if it does not exist

N

False

Max Page Count

{size in pages} - Limits the maximum number of pages (limits the size) of the database

N

0

Legacy Format

True - Use the more compatible legacy 3.x database format

False - Use the newer 3.3x database format which compresses numbers more effectively



留意其中几个参数,或许对你有用。比如 System.Data.SQLite 单方面支持密码保护,但其他的 SQLite 客户端无法读取这样的文件。