(1):C#讀取DB檔案
第一步 下載DLL檔案并安裝
DLL下載地址https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
選用版本sqlite-netFx46-setup-bundle-x64-2015-1.0.112.0.exe,適用框架.NET Framework 4.6(可以根據自己的需要選用),
下載后,系統默認安裝在C:\Program Files\System.Data.SQLite路徑下,拷貝System.Data.SQLite.dll檔案到工程檔案目錄下X:/Project/bin/debug,
在解決方案資源管理器中,選擇“參考”,右鍵后選擇“添加參考”,
如圖1,在參考管理器側邊欄選擇“瀏覽”后,再點擊“瀏覽”按鈕,安裝之前保存在工程檔案目錄下的System.Data.SQLite.dll,點擊“確定”后完成,

在程式中添加參考, 完成第一步
using System.Data.SQLite;
第二步 獲取資料
public DataTable GetDataTable(string strSQL, string path){
DataTable dt = null;
try {
SQLiteConnection conn = new SQLiteConnection(path);
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = strSQL;
cmd.Connection = conn;
SQLiteDataAdapter dao = new SQLiteDataAdapter(cmd);
dt = new DataTable();
dao.Fill(dt);
return dt;
}
catch{
MessageBox.Show("There is no such a datatable");
}
return dt;
}
其中strSQL是獲取db檔案中資料表的指令
string sSQL = "SELECT * FROM item_compound;";
這里的資料表名為"item_compound",
檔案路Path為
public static string DBPath = string.Format(@"Data Source={0}",
Application.StartupPath + @"\CCUS_supstr_temp.db");//the path of .db file
這里的db檔案名為“CCUS_supstr_temp.db”,
第三步 測驗代碼
private void FrmConvert_Load(object sender, EventArgs e){
string sSQL = "SELECT * FROM item_compound;";
DataTable dbt = GetDataTable(sSQL, DBPath);
this.dataGridView1.DataSource = dbt;
}
結果如圖2

2):C#操作SQLite資料庫(總結)
操作功能串列:
- 功能1:讀取所有表名/索引/視圖
- 功能2:讀取表資料
功能1:讀取所有表名/索引/視圖
每一個 SQLite 資料庫都有一個叫sqlit_master的表, 里面存盤著資料庫的資料結構(表結構、視圖結構、索引結構等),故通過讀取sqlit_master便可以獲取所有的表格資訊,
獲取表名
SELECT name FROM sqlite_master WHERE TYPE=‘table‘ ORDER BY name
獲取索引
SELECT name FROM sqlite_master WHERE TYPE=‘index‘ ORDER BY name
獲取視圖
SELECT name FROM sqlite_master WHERE TYPE=‘view‘ ORDER BY name
以獲取表名為例,完整代碼為
public DataSet GetTableNames(string path) {
string strSQL = "SELECT name FROM sqlite_master WHERE TYPE=‘table‘ ORDER BY name";
DataSet ds = null;
try {
SQLiteConnection conn = new SQLiteConnection(path);
SQLiteCommand cmd = new SQLiteCommand(strSQL, conn);
SQLiteDataAdapter reciever = new SQLiteDataAdapter(cmd);
ds = new DataSet();
reciever.Fill(ds);
return ds;
} catch {
MessageBox.Show("There is no data table");
}
return ds;
}16 DataSet dbnames = GetTableNames(DBPath);
注意此時回傳的ds包含的元素數量只有一個,所有表名以列向量的形式存盤在一張表中(即ds唯一的元素),
讀取表數量的代碼為
int tablecount = dbnames.Tables[0].Rows.Count;
讀取索引為X的表名
string tablename = dbnames.Table[0].Rows[X].ItemArray[0].ToString();//X starts from 0
功能2:讀取表資料
public DataTable GetDataTable(string strSQL, string path){
DataTable dt = null;
try {
SQLiteConnection conn = new SQLiteConnection(path);
SQLiteCommand cmd = new SQLiteCommand(strSQL,conn);
SQLiteDataAdapter reciever = new SQLiteDataAdapter(cmd);
dt = new DataTable();
reciever.Fill(dt);
return dt;
} catch{
MessageBox.Show("There is no such a datatable");
}
return dt;
}
其中strSQL是獲取db檔案中資料表的指令
string sSQL = "SELECT * FROM item_compound;";
這里的資料表名為"item_compound",
檔案路Path為
public static string DBPath = string.Format(@"Data Source={0}",
Application.StartupPath + @"\CCUS_supstr_temp.db");//the path of .db file
這里的db檔案名為“CCUS_supstr_temp.db”,
轉載自
https://www.ancii.com/ac9q53gmx/
https://www.ancii.com/amh64d6g4/
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/30388.html
標籤:C#
上一篇:幾種常見的加密方法的實作
