SQLite訪問封裝類)

來源:互聯網
上載者:User

  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原文。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.