分享一個SqliteHelper類
SQLite作為一個本地文件數據庫相當好用,小巧、快速、支持事務、關系型,甚至可以運行在Android上。在很久以前的一個項目中,我們用過它來將接收到的數據做本地統計,數據量很大,甚至于我們想自己搞個內存空間專門做緩存,緩存滿后再一點點地往SQLite中移,現在看起來是多余的,這也不符合開發的過程。在開發中,應該先把功能做出來,如果有性能問題,再找出解決方法。直接在SQLite中做插入而不是先在內存中做,它的效率已經達到了要求。
現在跟大家分享一個對SQLite操作的幫助類,使用它可以對本地SQLite數據庫進行方便的操作。
如有引用,注意寫明轉自:http://m.survivalescaperooms.com/wgp13x/p/3868916.html
關鍵詞:SQLite, C#, sqlite3.dll, SQLite Expert摘要:SQLite作為一個本地文件數據庫相當好用,小巧、快速、支持事務、關系型,在之前的一個項目中使用了它,現在把這使用經驗總結一下,分享給大家。 1 using System; 2 using System.Data.SQLite; 3 using System.Data; 4 using System.Data.Common; 5 6 namespace DXPlatformClientFramework.UC.StatAnalyzeCommon 7 { 8 public class SqliteHelper : IDisposable 9 { 10 public SQLiteConnection conn; 11 12 public void Dispose() 13 { 14 Dispose(true); 15 GC.SupPRessFinalize(this); 16 } 17 18 protected virtual void Dispose(bool disposing) 19 { 20 if(disposing) 21 if(conn != null) 22 { 23 conn.Dispose(); 24 conn = null; 25 } 26 } 27 28 ~SqliteHelper() 29 { 30 Dispose(false); 31 } 32 33 /// <summary> 34 /// 構造函數。 35 /// </summary> 36 /// <param name="dataBaseName">數據庫名</param> 37 public SqliteHelper(string dataBaseName) 38 { 39 string connString = string.Format(@"Data Source={0}", dataBaseName); 40 conn = new SQLiteConnection(connString); 41 conn.Open(); 42 } 43 44 /// <summary> 45 /// 手動打開數據庫。 46 /// </summary> 47 public void SqliteOpen() 48 { 49 if(conn != null && conn.State == ConnectionState.Closed) 50 conn.Open(); 51 } 52 53 /// <summary> 54 /// 通過執行SQL語句,獲取表中數據。 55 /// </summary> 56 /// <param name="sError">錯誤信息</param> 57 /// <param name="sSQL">執行的SQL語句</param> 58 public DataTable GetDataTable(out string sError, string sSQL) 59 { 60 DataTable dt = null; 61 sError = string.Empty; 62 try 63 { 64 SQLiteCommand cmd = newSQLiteCommand() { CommandText = sSQL, Connection = conn }; 65 SQLiteDataAdapter dao = newSQLiteDataAdapter(cmd); 66 dt = newDataTable(); 67 dao.Fill(dt); 68 } 69 catch(Exception e) 70 { 71 sError = e.Message; 72 } 73 return dt; 74 } 75 76 /// <summary> 77 /// 通過執行SQL語句,獲取表中數據個數。 78 /// </summary> 79 /// <param name="sError">錯誤信息</param> 80 /// <param name="sSQL">執行的SQL語句</param> 81 public int GetDataCount(out string sError, string sSQL) 82 { 83 DataTable dt = newDataTable(); 84 sError = string.Empty; 85 SQLiteCommand cmd = newSQLiteCommand() { CommandText = sSQL, Connection = conn }; 86 try 87 { 88 SQLiteDataAdapter dao = new SQLiteDataAdapter(cmd); 89 dao.Fill(dt); 90 cmd.Dispose(); 91 } 92 catch(Exception e) 93 { 94 sError = e.Message; 95 } 96 finally{ cmd.Dispose(); } 97 return int.Parse(dt.Rows[0][0].ToString()); 98 } 99 100 /// <summary>101 /// 通過執行SQL語句,執行insert,update,delete 動作,也可以使用事務。102 /// </summary>103 /// <param name="sError">錯誤信息</param>104 /// <param name="sSQL">執行的SQL語句</param>105 /// <param name="bUseTransaction">是否使用事務</param>106 public bool UpdateData(out string sError, string sSQL, bool bUseTransaction=false)108 {109 bool iResult = false;110 sError = string.Empty;111 if(!bUseTransaction)112 {113 try114 { 115 SQLiteCommand comm = new SQLiteCommand(conn) { CommandText = sSQL };116 iResult = comm.ExecuteNonQuery()>0;117 comm.Dispose();118 }119 catch(Exception ex)120 {121 sError = ex.Message;122 }123 }124 else// 使用事務125 {126 DbTransaction trans = null;127 trans = conn.BeginTransaction();128 SQLiteCommand comm = new SQLiteCommand(conn) { CommandText = sSQL };129 try130 { 131 iResult = comm.ExecuteNonQuery()>0;132 trans.Commit();133 }134 catch(Exception ex)135 {136 sError = ex.Message;137 iResult = false;138 trans.Rollback();139 }140 finally{comm.Dispose();trans.Dispose();}141 }142 return iResult;143 }144 145 /// <summary>146 /// 使用事務執行多條相同的帶參數的SQL語句。147 /// </summary>148 /// <param name="sqlString">SQL語句</param>149 /// <param name="sqLiteParameters">每次SQL執行的參數</param>150 public void ExecuteSqlTran(string sqlString, object[][] sqLiteParameters)151 {152 if(sqLiteParameters.Length == 0)153 return;154 using(DbTransaction trans = conn.BeginTransaction())155 {156 if(conn.State != ConnectionState.Open)157 conn.Open();158 SQLiteCommand cmd = conn.CreateCommand();159 cmd.Connection = conn;160 try161 {162 for(inti = 0; i < sqLiteParameters[0].Length; i++)163 {164 cmd.Parameters.Add(cmd.CreateParameter());165 }166 //循環167 foreach(object[] sqlParameters insqLiteParameters)168 {169 ExecuteSqlNonQuery(cmd, sqlString, sqlParameters);170 }171 trans.Commit();172 }173 catch(Exception ex)174 {175 trans.Rollback();176 throw;177 }178 finally179 {180 cmd.Dispose();trans.Dispose();181 }182 }183 }184 185 /// <summary>186 /// 不使用事務執行一條帶參數的SQL語句。187 /// </summary>188 /// <param name="sqlString">SQL語句</param>189 /// <param name="sqLiteParameters">SQL執行的參數</param>190 public void ExecuteSql(string sqlString, object[] sqLiteParameters)191 {192 if(conn.State != ConnectionState.Open)193 conn.Open();194 SQLiteCommand cmd = conn.CreateCommand();195 cmd.Connection = conn;196 cmd.CommandText = sqlString;197 try198 {199 for(inti = 0; i < sqLiteParameters.Length; i++)200 {201 cmd.Parameters.Add(cmd.CreateParameter());202 cmd.Parameters[i].Value = sqLiteParameters[i];203 }204 cmd.ExecuteNonQuery();205 }206 finally207 {208 cmd.Dispose();209 }210 }211 212 private void ExecuteSqlNonQuery(SQLiteCommand cmd, string cmdText, object[] cmdParms)214 {215 cmd.CommandText = cmdText;216 if(cmdParms != null)217 {218 for(int
新聞熱點
疑難解答