您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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(); } }
關于“C#如何操作SQLite數據庫幫助類”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。