c# Sqlite幫助類 最近有WPF做客戶端,需要離線操作存儲數據,在項目中考慮使用Sqlite嵌入式數據庫,在網上找了不少資料,最終整理出一個公共的幫助類。 Sqlite是一個非常小巧的數據庫,基本上具備關系型數據庫操作的大多數功能,Sql語法也大同小異。下面是我整理的幫助類代碼: 1.獲取 SQLiteConnection 對象,傳入數據庫有地址即可。 1 /// <summary><br> /// 獲得連接對象<br> /// </summary><br> /// <returns>SQLiteConnection</returns><br> public static SQLiteConnection GetSQLiteConnection()<br> {<br> //Sqlite數據庫地址<br> string str = AppDomain.CurrentDomain.BaseDirectory;<br> var con = new SQLiteConnection("Data Source=" + str + "DataBass\\InfoServiceDbB.db");<br> return con;<br> } 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(); } }