SQLite作為windows mobile的資料庫,是一種不錯的選擇。SQLite的為:SQLite,SQLite的ADO.net Provider為:System.Data.SQLite ,在發布程式時,請別忘記拷貝SQLite.Interop.065.DLL、system.data.sqlite.dll兩個檔案到安裝目錄下。
本文非原創,而是參考egmkang撰寫的 WM下訪問SQLite(一種替代SQL CE的解決方案) ,並根據我自己的編程需要和習慣做了少許修改,錯誤在所難免。下面實現的是一個SQLiteHelper封裝類:
using System.Data;using System.Data.SQLite;using System.IO;namespace MyHelper.DataAccess{ public class SQLiteHelper { private static string password = "***"; //請修改***為實際密碼 private static string dbFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly() .GetName().CodeBase) + "\\***.db"; //請修改***為實際SQLite資料庫名 private static string connectString = string.Format("Data Source =\"{0}\"", dbFilePath, password); private static SQLiteConnection myConnect = new SQLiteConnection(connectString); /**//// <summary> /// 取當前SQLite串連 /// </summary> /// <returns>當前SQLite串連</returns> public static SQLiteConnection GetConnection() { return myConnect; } /**//// <summary> /// 執行SQL語句,返回受影響的行數 /// </summary> /// <param name="commandString">SQL語句</param> /// <param name="parameters">SQL語句參數</param> /// <returns>受影響的行數</returns> public static int ExecuteNonQuery(string commandString, params SQLiteParameter[] parameters) { int result = 0; using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, null, commandString, parameters); result = command.ExecuteNonQuery(); command.Parameters.Clear(); } return result; } /**//// <summary> /// 執行帶事務的SQL語句,返回受影響的行數 /// </summary> /// <param name="transaction">SQL事務</param> /// <param name="commandString">SQL語句</param> /// <param name="parameters">SQL語句參數</param> /// <returns>受影響的行數</returns> public static int ExecuteNonQuery(SQLiteTransaction transaction, string commandString, params SQLiteParameter[] parameters) { int result = 0; using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, transaction, commandString, parameters); result = command.ExecuteNonQuery(); command.Parameters.Clear(); } return result; } /**//// <summary> /// 執行查詢,並返回結果集的第一行第一列的值,忽略其它所有的行和列 /// </summary> /// <param name="commandString">SQL語句</param> /// <param name="parameters">SQL語句參數</param> /// <returns>第一行第一列的值</returns> public static object ExecuteScalar(string commandString, params SQLiteParameter[] parameters) { object result; using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, null, commandString, parameters); result = command.ExecuteScalar(); } return result; } /**//// <summary> /// 執行SQL語句,返回結果集的DataReader /// </summary> /// <param name="commandString">SQL語句</param> /// <param name="parameters">SQL語句參數</param> /// <returns>結果集的DataReader</returns> public static SQLiteDataReader ExecuteReader(string commandString, params SQLiteParameter[] parameters) { SQLiteCommand command = new SQLiteCommand(); try { PrepareCommand(command, null, commandString, parameters); SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); command.Parameters.Clear(); return reader; } catch { throw; } } /**//// <summary> /// 預先處理Command對象,資料庫連結,事務,需要執行的對象,參數等的初始化 /// </summary> /// <param name="command">Command對象</param> /// <param name="transaction">transaction對象</param> /// <param name="commandString">SQL語句</param> /// <param name="parameters">SQL語句參數</param> private static void PrepareCommand(SQLiteCommand command, SQLiteTransaction transaction, string commandString, params SQLiteParameter[] parameters) { if (myConnect.State != ConnectionState.Open) myConnect.Open(); command.Connection = myConnect; command.CommandText = commandString; if (transaction != null) command.Transaction = transaction; if (parameters != null && parameters.Length > 0) { command.Parameters.AddRange(parameters); } } }}
為了提高效率,我將資料庫連接修改為長串連。這樣,只有第一次串連時會比較耗時,以後的操作就比較快了。如果本文有錯誤的地方,請大家參考egmkang原文。