C#操作SQLite資料庫
阿新 • • 發佈:2021-07-21
C#操作SQLite資料庫幫助類詳解
更新時間:2017年07月11日 09:31:40 作者:Only Version 這篇文章主要介紹了C#操作SQLite資料庫幫助類,詳細分析了C#針對sqlite資料庫的連線、查詢、分頁等各種常見操作的實現與封裝技巧,需要的朋友可以參考下:本文例項講述了C#操作SQLite資料庫幫助類。分享給大家供大家參考,具體如下:
最近有WPF做客戶端,需要離線操作儲存資料,在專案中考慮使用Sqlite嵌入式資料庫,在網上找了不少資料,最終整理出一個公共的幫助類。
Sqlite是一個非常小巧的資料庫,基本上具備關係型資料庫操作的大多數功能,Sql語法也大同小異。下面是我整理的幫助類程式碼:
1.獲取 SQLiteConnection 物件,傳入資料庫有地址即可。
/// <summary>
/// 獲得連線物件
/// </summary>
/// <returns>SQLiteConnection</returns>
public static SQLiteConnection GetSQLiteConnection()
{
//Sqlite資料庫地址
string str = AppDomain.CurrentDomain.BaseDirectory;
var con = new SQLiteConnection("Data Source=" + str + "DataBass\\InfoServiceDbB.db");
return con;
}
2.準備操作命令引數,構造SQLiteCommand 物件:
/// <summary>
/// 準備操作命令引數
/// </summary>
/// <param name="cmd">SQLiteCommand</param>
/// <param name="conn">SQLiteConnection</param>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="data">引數陣列</param>
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, Dictionary<String, String> data)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Parameters.Clear();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 30;
if (data!=null&&data.Count >= 1)
{
foreach (KeyValuePair<String, String> val in data)
{
cmd.Parameters.AddWithValue(val.Key, val.Value);
}
}
}
3.查詢,返回DataSet
/// <summary>
/// 查詢,返回DataSet
/// </summary>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="data">引數陣列</param>
/// <returns>DataSet</returns>
public static DataSet ExecuteDataset(string cmdText, Dictionary<string, string> data)
{
var ds = new DataSet();
using (SQLiteConnection connection = GetSQLiteConnection())
{
var command = new SQLiteCommand();
PrepareCommand(command, connection, cmdText, data);
var da = new SQLiteDataAdapter(command);
da.Fill(ds);
}
return ds;
}
4.查詢,返回DataTable
/// <summary>
/// 查詢,返回DataTable
/// </summary>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="data">引數陣列</param>
/// <returns>DataTable</returns>
public static DataTable ExecuteDataTable(string cmdText, Dictionary<string, string> data)
{
var dt = new DataTable();
using (SQLiteConnection connection = GetSQLiteConnection())
{
var command = new SQLiteCommand();
PrepareCommand(command, connection, cmdText, data);
SQLiteDataReader reader = command.ExecuteReader();
dt.Load(reader);
}
return dt;
}
5.返回一行資料 DataRow
/// <summary>
/// 返回一行資料
/// </summary>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="data">引數陣列</param>
/// <returns>DataRow</returns>
public static DataRow ExecuteDataRow(string cmdText, Dictionary<string, string> data)
{
DataSet ds = ExecuteDataset(cmdText, data);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
return ds.Tables[0].Rows[0];
return null;
}
6.執行資料庫操作
/// <summary>
/// 執行資料庫操作
/// </summary>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="data">傳入的引數</param>
/// <returns>返回受影響的行數</returns>
public static int ExecuteNonQuery(string cmdText, Dictionary<string, string> data)
{
using (SQLiteConnection connection = GetSQLiteConnection())
{
var command = new SQLiteCommand();
PrepareCommand(command, connection, cmdText, data);
return command.ExecuteNonQuery();
}
}
7.返回SqlDataReader物件
/// <summary>
/// 返回SqlDataReader物件
/// </summary>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="data">傳入的引數</param>
/// <returns>SQLiteDataReader</returns>
public static SQLiteDataReader ExecuteReader(string cmdText, Dictionary<string, string> data)
{
var command = new SQLiteCommand();
SQLiteConnection connection = GetSQLiteConnection();
try
{
PrepareCommand(command, connection, cmdText, data);
SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
catch
{
connection.Close();
command.Dispose();
throw;
}
}
8.返回結果集中的第一行第一列,忽略其他行或列
/// <summary>
/// 返回結果集中的第一行第一列,忽略其他行或列
/// </summary>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="data">傳入的引數</param>
/// <returns>object</returns>
public static object ExecuteScalar(string cmdText, Dictionary<string, string> data)
{
using (SQLiteConnection connection = GetSQLiteConnection())
{
var cmd = new SQLiteCommand();
PrepareCommand(cmd, connection, cmdText, data);
return cmd.ExecuteScalar();
}
}
9.分頁查詢
/// <summary>
/// 分頁查詢
/// </summary>
/// <param name="recordCount">總記錄數</param>
/// <param name="pageIndex">頁牽引</param>
/// <param name="pageSize">頁大小</param>
/// <param name="cmdText">Sql命令文字</param>
/// <param name="countText">查詢總記錄數的Sql文字</param>
/// <param name="data">命令引數</param>
/// <returns>DataSet</returns>
public static DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, Dictionary<string, string> data)
{
if (recordCount < 0)
recordCount = int.Parse(ExecuteScalar(countText, data).ToString());
var ds = new DataSet();
using (SQLiteConnection connection = GetSQLiteConnection())
{
var command = new SQLiteCommand();
PrepareCommand(command, connection, cmdText, data);
var da = new SQLiteDataAdapter(command);
da.Fill(ds, (pageIndex - 1) * pageSize, pageSize, "result");
}
return ds;
}
10.重新組織資料庫
當你從SQLite資料庫中刪除資料時, 未用的磁碟空間將會加入一個內部的“自由列表”中。
當你下次插入資料時,這部分空間可以重用。磁碟空間不會丟失, 但也不會返還給作業系統。
如果刪除了大量資料,而又想縮小資料庫檔案佔用的空間,執行 VACUUM 命令。 VACUUM 將會從頭重新組織資料庫
你可以在你的程式中約定一個時間間隔執行一次重新組織資料庫的操作,節約空間
public void ResetDataBass()
{
using (SQLiteConnection conn = GetSQLiteConnection())
{
var cmd = new SQLiteCommand();
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Parameters.Clear();
cmd.Connection = conn;
cmd.CommandText = "vacuum";
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 30;
cmd.ExecuteNonQuery();
}
}