C# 程式中使用 SQLite 資料庫
現在又要用到 C# 了,所以決意瞭解下 C# 中如何使用 SQLite,之所以選擇 SQLite 作為自己的檔案型資料,主要是看中它的非常小型、單檔案、嵌入型,更支援多程序訪問。
SQLite 官方網站:http://www.sqlite.org,你可以在那裡下載到一個 sqlite3.exe,用命令列模式來管理你的資料庫檔案。或用其他比較好的管理工具:sqliteman(也支援 UTF-8) 、sqlitebrowser 和 SQLite Administrator。我比較喜歡 sqlitebrowser,但更推薦可設定字符集的 SQLite Expert(可下載到免費的個人版)。
既然是嵌入式資料庫,只要些動態庫就行,如 dll 原生庫,jar 包或其他語言的元件庫形式,.NET 中也不例如,要用到
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 客戶端無法讀取這樣的檔案。