SQLite資料庫幫助靜態類
阿新 • • 發佈:2018-11-20
using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.SQLite; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MyJobSearch.Common { /// <summary> /// 本類為SQLite資料庫幫助靜態類,使用時只需直接呼叫即可,無需例項化 /// </summary> public static class SQLiteHelper { /// <summary> /// 資料庫連線字串 /// </summary> public static string connectionString = "Data Source=" + Application.StartupPath + "\\" + System.Configuration.ConfigurationSettings.AppSettings["Contr"]; #region 執行資料庫操作(新增、更新或刪除),返回影響行數 /// <summary> /// 執行資料庫操作(新增、更新或刪除) /// </summary> /// <param name="cmd">SqlCommand物件</param> /// <returns>所受影響的行數</returns> public static int ExecuteNonQuery(SQLiteCommand cmd) { int result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText); try { result = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 執行資料庫操作(新增、更新或刪除) /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <returns>所受影響的行數</returns> public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text) { int result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText); try { result = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 執行資料庫操作(新增、更新或刪除) /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <param name="cmdParms">SQL引數物件</param> /// <returns>所受影響的行數</returns> public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { int result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText,cmdParms); try { result = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } #endregion #region 執行資料庫操作(新增、更新或刪除)同時返回執行後查詢所得的第1行第1列資料 /// <summary> /// 執行資料庫操作(新增、更新或刪除)同時返回執行後查詢所得的第1行第1列資料 /// </summary> /// <param name="cmd">SqlCommand物件</param> /// <returns>查詢所得的第1行第1列資料</returns> public static object ExecuteScalar(SQLiteCommand cmd) { object result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText); try { result = cmd.ExecuteScalar(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 執行資料庫操作(新增、更新或刪除)同時返回執行後查詢所得的第1行第1列資料 /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <returns>查詢所得的第1行第1列資料</returns> public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text) { object result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText); try { result = cmd.ExecuteScalar(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 執行資料庫操作(新增、更新或刪除)同時返回執行後查詢所得的第1行第1列資料 /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <param name="cmdParms">SQL引數物件</param> /// <returns>查詢所得的第1行第1列資料</returns> public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { object result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText, cmdParms); try { result = cmd.ExecuteScalar(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } #endregion #region 執行資料庫查詢,返回SqlDataReader物件 /// <summary> /// 執行資料庫查詢,返回SqlDataReader物件 /// </summary> /// <param name="cmd">SqlCommand物件</param> /// <returns>SqlDataReader物件</returns> public static DbDataReader ExecuteReader(SQLiteCommand cmd) { DbDataReader reader = null; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText); try { reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { throw ex; } return reader; } /// <summary> /// 執行資料庫查詢,返回SqlDataReader物件 /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <returns>SqlDataReader物件</returns> public static DbDataReader ExecuteReader(string commandText, CommandType commandType = CommandType.Text) { DbDataReader reader = null; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText); try { reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { throw ex; } return reader; } /// <summary> /// 執行資料庫查詢,返回SqlDataReader物件 /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <param name="cmdParms">SQL引數物件</param> /// <returns>SqlDataReader物件</returns> public static DbDataReader ExecuteReader(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { DbDataReader reader = null; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms); try { reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { throw ex; } return reader; } #endregion #region 執行資料庫查詢,返回DataSet物件 /// <summary> /// 執行資料庫查詢,返回DataSet物件 /// </summary> /// <param name="cmd">SqlCommand物件</param> /// <returns>DataSet物件</returns> public static DataSet ExecuteDataSet(SQLiteCommand cmd) { DataSet ds = new DataSet(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { if (cmd.Connection != null) { if (cmd.Connection.State == ConnectionState.Open) { cmd.Connection.Close(); } } } return ds; } /// <summary> /// 執行資料庫查詢,返回DataSet物件 /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <returns>DataSet物件</returns> public static DataSet ExecuteDataSet(string commandText, CommandType commandType = CommandType.Text) { if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); DataSet ds = new DataSet(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { if (con != null) { if (con.State == ConnectionState.Open) { con.Close(); } } } return ds; } /// <summary> /// 執行資料庫查詢,返回DataSet物件 /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <param name="cmdParms">SQL引數物件</param> /// <returns>DataSet物件</returns> public static DataSet ExecuteDataSet(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); DataSet ds = new DataSet(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { if (con != null) { if (con.State == ConnectionState.Open) { con.Close(); } } } return ds; } #endregion #region 執行資料庫查詢,返回DataTable物件 /// <summary> /// 執行資料庫查詢,返回DataTable物件 /// </summary> /// <param name="commandText">執行語句或儲存過程名</param> /// <param name="commandType">執行型別(預設語句)</param> /// <returns>DataTable物件</returns> public static DataTable ExecuteDataTable(string commandText, CommandType commandType = CommandType.Text) { if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); DataTable dt = new DataTable(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(dt); } catch (Exception ex) { throw ex; } finally { if (con != null) { if (con.State == ConnectionState.Open) { con.Close(); } } } return dt; } #endregion #region 通用分頁查詢方法 /// <summary> /// 通用分頁查詢方法 /// </summary> /// <param name="tableName">表名</param> /// <param name="strColumns">查詢欄位名</param> /// <param name="strWhere">where條件</param> /// <param name="strOrder">排序條件</param> /// <param name="pageSize">每頁資料數量</param> /// <param name="currentIndex">當前頁數</param> /// <param name="recordOut">資料總量</param> /// <returns>DataTable資料表</returns> public static DataTable SelectPaging(string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut) { DataTable dt = new DataTable(); recordOut = Convert.ToInt32(ExecuteScalar("select count(*) from " + tableName, CommandType.Text)); string pagingTemplate = "select {0} from {1} where {2} order by {3} limit {4} offset {5} "; int offsetCount = (currentIndex - 1) * pageSize; string commandText = String.Format(pagingTemplate, strColumns, tableName, strWhere, strOrder, pageSize.ToString(), offsetCount.ToString()); using (DbDataReader reader = ExecuteReader(commandText, CommandType.Text)) { if (reader != null) { dt.Load(reader); } } return dt; } #endregion #region 預處理Command物件,資料庫連結,事務,需要執行的物件,引數等的初始化 /// <summary> /// 預處理Command物件,資料庫連結,事務,需要執行的物件,引數等的初始化 /// </summary> /// <param name="cmd">Command物件</param> /// <param name="conn">Connection物件</param> /// <param name="trans">Transcation物件</param> /// <param name="useTrans">是否使用事務</param> /// <param name="cmdType">SQL字串執行型別</param> /// <param name="cmdText">SQL Text</param> /// <param name="cmdParms">SQLiteParameters to use in the command</param> private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, ref SQLiteTransaction trans, bool useTrans, CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (useTrans) { trans = conn.BeginTransaction(IsolationLevel.ReadCommitted); cmd.Transaction = trans; } cmd.CommandType = cmdType; if (cmdParms != null) { foreach (SQLiteParameter parm in cmdParms) cmd.Parameters.Add(parm); } } #endregion } }