C#中使用SQLite數(shù)據(jù)庫的方法介紹
推薦以下2款:
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必備的小工具。下載地址:http://fishcodelib.com/Database.htm 腳本之家下載地址 http://www.dhdzp.com/database/41238.html
建議以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/>");
}
可以看到代碼精簡了很多。
【SQLite相關有用的鏈接地址】
SQLite官方網(wǎng)站:http://www.sqlite.org/
SQLite內置核心函數(shù)參考文檔:http://www.sqlite.org/lang_corefunc.html
SQLite日期時間函數(shù)參考文檔:http://www.sqlite.org/lang_datefunc.html
SQLite數(shù)學函數(shù)參考文檔:http://www.sqlite.org/lang_aggfunc.html
SQLite相關SQL語法參考文檔:http://www.sqlite.org/lang.html
System.Data.SQLite.dll數(shù)據(jù)訪問驅動下載地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
相關文章
c# static 靜態(tài)數(shù)據(jù)成員
靜態(tài)成員屬于類所有,為各個類的實例所公用,無論類創(chuàng)建了幾多實例,類的靜態(tài)成員在內存中只占同一塊區(qū)域。2009-06-06
淺談誰都能看懂的單點登錄(SSO)實現(xiàn)方式(附源碼)
這篇文章主要介紹了淺談誰都能看懂的單點登錄(SSO)實現(xiàn)方式(附源碼),具有一定的參考價值,有需要的可以了解一下。2016-12-12
asp.net動態(tài)添加js文件調用到網(wǎng)頁的方法
這篇文章主要介紹了asp.net動態(tài)添加js文件調用到網(wǎng)頁的方法,涉及asp.net動態(tài)添加js的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
基于ASP.NET+easyUI框架實現(xiàn)圖片上傳功能(表單)
這篇文章主要介紹了基于ASP.NET+easyUI框架實現(xiàn)圖片上傳功能的相關資料,需要的朋友可以參考下2016-06-06
.Net Core+Angular Cli/Angular4開發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細介紹了.Net Core+Angular Cli/Angular4開發(fā)環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
GridView控件實現(xiàn)數(shù)據(jù)的修改(第9節(jié))
這篇文章主要介紹了GridView控件實現(xiàn)數(shù)據(jù)的修改,需要的朋友可以參考下2015-08-08

