SQLite是遵守ACID的關系型數(shù)據(jù)庫管理系統(tǒng),隨著移動互聯(lián)的發(fā)展,現(xiàn)在得到了更廣泛的使用。那么你知道如何在C#中使用SQLite數(shù)據(jù)庫嗎?下面就隨小編一起去看看吧。
Navicat for SQLite:功能非常強大,幾乎包含了數(shù)據(jù)庫管理工具的所有必需功能,操作簡單,容易上手。唯一的缺點是不能打開由System.Data.SQLite.dll加密過的數(shù)據(jù)庫。
Database.Net:臺灣人用.net開發(fā)的全能數(shù)據(jù)庫管理工具,可以管理多種數(shù)據(jù)庫,包括MSSQL、MYSQL、IBM DB2、Oracle、Access、Excel、OleDb、Odbc等十多種數(shù)據(jù)庫(或數(shù)據(jù)接口),功能沒有Navicat那么多,只包含最基本功能。對SQLite而言,Database.Net最大的優(yōu)點是支持打開由System.Data.SQLite.dll加密過的數(shù)據(jù)庫,且可以隨時對數(shù)據(jù)庫設置密碼,是.net下開發(fā)SQLite必備的小工具。
建議以Navicat for SQLite為主,Database.Net為輔,只要涉及到數(shù)據(jù)庫加密時才用后者。
【操作SQLite實例】
操作SQlite的方法基本同其他數(shù)據(jù)庫相同,但有一些區(qū)別:
『例1』整數(shù)似乎都是Int64的。
查詢出網(wǎng)站App_Data目錄下“省市.db”數(shù)據(jù)庫中city表的總記錄數(shù)
復制代碼 代碼如下:
SQLiteConnection cn = new SQLiteConnection("Data Source=|DataDirectory|省市.db;Version=3");
SQLiteCommand cmd = new SQLiteCommand("select count(*) from city", cn);
cn.Open();
int recordCount = (int)(Int64)cmd.ExecuteScalar();
cn.Close();
Response.Write(recordCount);
SQLite中count函數(shù)返回的是一個Int64的整數(shù),這一點同MSSQL、Access等不同。實際上,經(jīng)過有限的使用發(fā)現(xiàn),似乎所有INTEGER字段的返回值都是Int64,這一點未經(jīng)過有效證實。ExecuteScalar方法返回一個object實例,按照C#規(guī)定,拆箱時進行標準轉換,必須轉換成該object實例實際存儲的格式,因此分兩步,先轉換成Int64,再轉換成int。當然用.net中某些高級轉換器如Convert.ToInt32方法只要一步就可以了。
『例2』批量增刪改時需要用事務,否則效率很低。
批量插入1000條記錄,每條記錄只有簡單的id、name、password三個字段:
?
復制代碼 代碼如下:
?
SQLiteConnection cn = new SQLiteConnection("Data Source=c://測試.db3;Version=3;password=12345");
SQLiteCommand cmd = new SQLiteCommand("select count(*) from test", cn);
cn.Open();
int recordCount = (int)(Int64)cmd.ExecuteScalar();
Response.Write("當前的總記錄數(shù):" + recordCount + "<br/>");
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into test values(@id,@name,@password)";
cmd.Parameters.AddWithValue("@id", i);
cmd.Parameters.AddWithValue("@name", "姓名" + i);
cmd.Parameters.AddWithValue("@password", (i * 2).ToString());
cmd.ExecuteNonQuery();
}
cmd.CommandText = "select count(*) from test";
recordCount = (int)(Int64)cmd.ExecuteScalar();
cn.Close();
Response.Write("當前的總記錄數(shù):" + recordCount + "<br/>");
經(jīng)過測試,這段代碼中的for循環(huán)花費了70000~90000毫秒,一分鐘多!
改用事務執(zhí)行:
?
復制代碼 代碼如下:
?
SQLiteConnection cn = new SQLiteConnection("Data Source=c://測試.db3;Version=3;password=12345");
SQLiteCommand cmd = new SQLiteCommand("select count(*) from test", cn);
cn.Open();
int recordCount = (int)(Int64)cmd.ExecuteScalar();
Response.Write("當前的總記錄數(shù):" + recordCount + "<br/>");
SQLiteTransaction tran = cn.BeginTransaction();
cmd.Transaction = tran;
try
{
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into test values(@id,@name,@password)";
cmd.Parameters.AddWithValue("@id", i);
cmd.Parameters.AddWithValue("@name", "姓名" + i);
cmd.Parameters.AddWithValue("@password", (i * 2).ToString());
cmd.ExecuteNonQuery();
}
tran.Commit();
}
catch
{
tran.Rollback();
Response.Write("執(zhí)行出錯!");
}
finally
{
cmd.CommandText = "select count(*) from test";
recordCount = (int)(Int64)cmd.ExecuteScalar();
cn.Close();
Response.Write("當前的總記錄數(shù):" + recordCount + "<br/>");
}
經(jīng)過測試,這段代碼中的try部分只用了100~150毫秒!開啟事務后,效率非常高!
『例3』一般開發(fā)中可以編寫自己的數(shù)據(jù)庫通用操作類,進一步封裝ADO.NET。
如上面用事務操作的代碼,改用數(shù)據(jù)庫通用操作類后:
?
復制代碼 代碼如下:
?
SQLiteData md = new SQLiteData("Data Source=c://測試.db3;Version=3;password=12345");
int recordCount = (int)(Int64)md.ExecuteScalar("select count(*) from test");
Response.Write("當前的總記錄數(shù):" + recordCount + "<br/>");
md.CreateTransaction();
try
{
for (int i = 0; i < 1000; i++)
md.ExecuteNonQuery("insert into test values(@id,@name,@password)", "@id", i, "@name", "姓名" + i, "@password", (i * 2).ToString());
md.CommitTransaction();
}
catch
{
md.RollBack();
Response.Write("執(zhí)行出錯!");
}
finally
{
recordCount = (int)(Int64)md.ExecuteScalar("select count(*) from test");
md.Close();
Response.Write("當前的總記錄數(shù):" + recordCount + "<br/>");
}
?
看完后你是不是學會了如何在C#中使用SQLite數(shù)據(jù)庫呢?當我們在C#中使用SQLite數(shù)據(jù)庫之后就可以看到代碼精簡了很多。
?