佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > C#中使用SQLite數(shù)據(jù)庫的方法介紹

C#中使用SQLite數(shù)據(jù)庫的方法介紹

熱門標簽:電子圍欄 科大訊飛語音識別系統(tǒng) 服務器配置 銀行業(yè)務 阿里云 團購網(wǎng)站 Linux服務器 Mysql連接數(shù)設置
【SQLite管理工具簡介】
推薦以下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 腳本之家下載地址 https://www.jb51.net/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內(nèi)置核心函數(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#操作SQLite數(shù)據(jù)庫方法小結(創(chuàng)建,連接,插入,查詢,刪除等)
  • ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實例
  • c#幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入(SqlServer、Oracle、SQLite和MySql)
  • C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
  • C#基于SQLiteHelper類似SqlHelper類實現(xiàn)存取Sqlite數(shù)據(jù)庫的方法
  • C#操作SQLite數(shù)據(jù)庫之讀寫數(shù)據(jù)庫的方法
  • C#/.Net 中快速批量給SQLite數(shù)據(jù)庫插入測試數(shù)據(jù)
  • C#中嵌入SQLite數(shù)據(jù)庫的簡單方法
  • C#簡單查詢SQLite數(shù)據(jù)庫是否存在數(shù)據(jù)的方法
  • C# SQLite數(shù)據(jù)庫入門使用說明

標簽:棗莊 萍鄉(xiāng) 衡水 江蘇 衢州 廣元 蚌埠 大理

巨人網(wǎng)絡通訊聲明:本文標題《C#中使用SQLite數(shù)據(jù)庫的方法介紹》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    恩平市| 山丹县| 岐山县| 霍州市| 池州市| 吉安市| 湖北省| 邓州市| 方正县| 门源| 惠水县| 玉树县| 汕尾市| 富平县| 江达县| 平湖市| 上杭县| 城口县| 齐河县| 根河市| 夹江县| 卓尼县| 贡觉县| 城口县| 普格县| 建水县| 分宜县| 汉川市| 安陆市| 浮山县| 荆州市| 泰来县| 囊谦县| 丰城市| 富锦市| 德令哈市| 康乐县| 凯里市| 惠来县| 巴林左旗| 枣阳市|