程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> C#中使用SQLite數據庫的方法介紹

C#中使用SQLite數據庫的方法介紹

編輯:ASP.NET基礎
【SQLite管理工具簡介】
推薦以下2款:
Navicat for SQLite:功能非常強大,幾乎包含了數據庫管理工具的所有必需功能,操作簡單,容易上手。唯一的缺點是不能打開由System.Data.SQLite.dll加密過的數據庫。
Database.Net:台灣人用.net開發的全能數據庫管理工具,可以管理多種數據庫,包括MSSQL、MYSQL、IBM DB2、Oracle、Access、Excel、OleDb、Odbc等十多種數據庫(或數據接口),功能沒有Navicat那麼多,只包含最基本功能。對SQLite而言,Database.Net最大的優點是支持打開由System.Data.SQLite.dll加密過的數據庫,且可以隨時對數據庫設置密碼,是.net下開發SQLite必備的小工具。下載地址:http://fishcodelib.com/Database.htm 下載地址 http://www.jb51.net/database/41238.html
建議以Navicat for SQLite為主,Database.Net為輔,只要涉及到數據庫加密時才用後者。
【操作SQLite實例】
操作SQlite的方法基本同其他數據庫相同,但有一些區別:
『例1』整數似乎都是Int64的。
查詢出網站App_Data目錄下“省市.db”數據庫中city表的總記錄數
復制代碼 代碼如下:
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函數返回的是一個Int64的整數,這一點同MSSQL、Access等不同。實際上,經過有限的使用發現,似乎所有INTEGER字段的返回值都是Int64,這一點未經過有效證實。ExecuteScalar方法返回一個object實例,按照C#規定,拆箱時進行標准轉換,必須轉換成該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("當前的總記錄數:" + 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("當前的總記錄數:" + recordCount + "<br/>");

經過測試,這段代碼中的for循環花費了70000~90000毫秒,一分鐘多!
改用事務執行:
復制代碼 代碼如下:
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("當前的總記錄數:" + 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("執行出錯!");
}
finally
{
cmd.CommandText = "select count(*) from test";
recordCount = (int)(Int64)cmd.ExecuteScalar();
cn.Close();
Response.Write("當前的總記錄數:" + recordCount + "<br/>");
}

經過測試,這段代碼中的try部分只用了100~150毫秒!開啟事務後,效率非常高!
『例3』一般開發中可以編寫自己的數據庫通用操作類,進一步封裝ADO.NET。
如上面用事務操作的代碼,改用數據庫通用操作類後:
復制代碼 代碼如下:
SQLiteData md = new SQLiteData("Data Source=c:\\測試.db3;Version=3;password=12345");
int recordCount = (int)(Int64)md.ExecuteScalar("select count(*) from test");
Response.Write("當前的總記錄數:" + 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("執行出錯!");
}
finally
{
recordCount = (int)(Int64)md.ExecuteScalar("select count(*) from test");
md.Close();
Response.Write("當前的總記錄數:" + recordCount + "<br/>");
}

可以看到代碼精簡了很多。

【SQLite相關有用的鏈接地址】

SQLite官方網站:http://www.sqlite.org/

SQLite內置核心函數參考文檔:http://www.sqlite.org/lang_corefunc.html

SQLite日期時間函數參考文檔:http://www.sqlite.org/lang_datefunc.html

SQLite數學函數參考文檔:http://www.sqlite.org/lang_aggfunc.html

SQLite相關SQL語法參考文檔:http://www.sqlite.org/lang.html

System.Data.SQLite.dll數據訪問驅動下載地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved