SQLite做爲一個本地文件數據庫至關好用,小巧、快速、支持事務、關係型,甚至能夠運行在Android上。在好久之前的一個項目中,咱們用過它來將接收到的數據作本地統計,數據量很大,甚至於咱們想本身搞個內存空間專門作緩存,緩存滿後再一點點地往SQLite中移,如今看起來是多餘的,這也不符合開發的過程。在開發中,應該先把功能作出來,若是有性能問題,再找出解決方法。直接在SQLite中作插入而不是先在內存中作,它的效率已經達到了要求。java
如今跟你們分享一個對SQLite操做的幫助類,使用它能夠對本地SQLite數據庫進行方便的操做。sql
若有引用,注意寫明轉自:http://www.cnblogs.com/wgp13x/p/3868916.html數據庫
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 try 114 { 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 try 130 { 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 try 161 { 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 finally 179 { 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 try 198 { 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 finally 207 { 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(inti = 0; i < cmdParms.Length; i++ ) 219 { 220 cmd.Parameters[i].Value = cmdParms[i]; 221 } 222 } 223 cmd.ExecuteNonQuery(); 224 } 225 } 226 }