SQLite-C#-幫助類
阿新 • • 發佈:2017-06-04
created class false apt 影響 scala text emp 集合
1 public static class SQLiteHelper 2 { 3 private static string connectionString = string.Empty; 4 5 #region void SetConnectionString(string datasource, string password, int version = 3) 根據數據源、密碼、版本號設置連接字符串 6 /// <summary> 7 /// 根據數據源、密碼、版本號設置連接字符串。8 /// </summary> 9 /// <param name="datasource">數據源。</param> 10 /// <param name="password">密碼。</param> 11 /// <param name="version">版本號(缺省為3)。</param> 12 public static void SetConnectionString(string datasource, string password, intversion = 3) 13 { 14 connectionString = string.Format("Data Source={0};Version={1};password={2};Pooling=true;FailIfMissing=false", 15 datasource, version, password); 16 } 17 #endregion 18 19 #region void CreateDB(string dbName) 創建一個數據庫文件。如果存在同名數據庫文件,則會覆蓋。 20/// <summary> 21 /// 創建一個數據庫文件。如果存在同名數據庫文件,則會覆蓋。 22 /// </summary> 23 /// <param name="dbName">數據庫文件名。為null或空串時不創建。</param> 24 /// <param name="password">(可選)數據庫密碼,默認為空。</param> 25 /// <exception cref="Exception"></exception> 26 public static void CreateDB(string dbName) 27 { 28 if (!string.IsNullOrEmpty(dbName)) 29 { 30 try { SQLiteConnection.CreateFile(dbName); } 31 catch (Exception) { throw; } 32 } 33 } 34 #endregion 35 36 #region void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params SQLiteParameter[] parameters)準備操作命令參數 37 /// <summary> 38 /// 準備操作命令參數 39 /// </summary> 40 /// <param name="cmd">SQLiteCommand</param> 41 /// <param name="conn">SQLiteConnection</param> 42 /// <param name="cmdText">Sql命令文本</param> 43 private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params SQLiteParameter[] parameters) 44 { 45 if (conn.State != ConnectionState.Open) 46 conn.Open(); 47 cmd.Parameters.Clear(); 48 cmd.Connection = conn; 49 cmd.CommandText = cmdText; 50 cmd.CommandType = CommandType.Text; 51 cmd.CommandTimeout = 30; 52 if (parameters.Length != 0) 53 { 54 cmd.Parameters.AddRange(parameters); 55 } 56 } 57 #endregion 58 59 #region ExecuteNonQuery(string sql, params SQLiteParameter[] parameters) 對SQLite數據庫執行增刪改操作,返回受影響的行數。 60 /// <summary> 61 /// 對SQLite數據庫執行增刪改操作,返回受影響的行數。 62 /// </summary> 63 /// <param name="sql">要執行的增刪改的SQL語句。</param> 64 /// <param name="parameters">執行增刪改語句所需要的參數,參數必須以它們在SQL語句中的順序為準。</param> 65 /// <returns></returns> 66 /// <exception cref="Exception"></exception> 67 public static int ExecuteNonQuery(string sql, params SQLiteParameter[] parameters) 68 { 69 int affectedRows = 0; 70 using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 71 { 72 using (SQLiteCommand command = new SQLiteCommand()) 73 { 74 try 75 { 76 PrepareCommand(command, connection, sql, parameters); 77 affectedRows = command.ExecuteNonQuery(); 78 } 79 catch (Exception) { throw; } 80 } 81 } 82 return affectedRows; 83 } 84 #endregion 85 86 #region void ExecuteNonQueryBatch(List<KeyValuePair<string, SQLiteParameter[]>> list) 批量處理數據操作語句 87 /// <summary> 88 /// 批量處理數據操作語句。 89 /// </summary> 90 /// <param name="list">SQL語句集合。</param> 91 /// <exception cref="Exception"></exception> 92 public static void ExecuteNonQueryBatch(List<KeyValuePair<string, SQLiteParameter[]>> list) 93 { 94 using (SQLiteConnection conn = new SQLiteConnection(connectionString)) 95 { 96 if (conn.State != ConnectionState.Open) 97 conn.Open(); 98 using (SQLiteTransaction tran = conn.BeginTransaction()) 99 { 100 using (SQLiteCommand cmd = new SQLiteCommand(conn)) 101 { 102 try 103 { 104 foreach (var item in list) 105 { 106 cmd.CommandText = item.Key; 107 if (item.Value != null) 108 { 109 cmd.Parameters.AddRange(item.Value); 110 } 111 cmd.ExecuteNonQuery(); 112 } 113 tran.Commit(); 114 } 115 catch (Exception) { tran.Rollback(); throw; } 116 } 117 } 118 } 119 } 120 #endregion 121 122 #region object ExecuteScalar(string sql, params SQLiteParameter[] parameters) 執行查詢語句,並返回第一個結果。 123 /// <summary> 124 /// 執行查詢語句,並返回第一個結果。 125 /// </summary> 126 /// <param name="sql">查詢語句。</param> 127 /// <returns>查詢結果。</returns> 128 /// <exception cref="Exception"></exception> 129 public static object ExecuteScalar(string sql, params SQLiteParameter[] parameters) 130 { 131 using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 132 { 133 using (SQLiteCommand command = new SQLiteCommand()) 134 { 135 try 136 { 137 PrepareCommand(command, connection, sql, parameters); 138 139 return command.ExecuteScalar(); 140 } 141 catch (Exception) { throw; } 142 } 143 } 144 } 145 #endregion 146 147 #region DataTable ExecuteQuery(string sql, params SQLiteParameter[] parameters)執行一個查詢語句,返回一個包含查詢結果的DataTable。 148 /// <summary> 149 /// 執行一個查詢語句,返回一個包含查詢結果的DataTable。 150 /// </summary> 151 /// <param name="sql">要執行的查詢語句。</param> 152 /// <param name="parameters">執行SQL查詢語句所需要的參數,參數必須以它們在SQL語句中的順序為準。</param> 153 /// <returns></returns> 154 /// <exception cref="Exception"></exception> 155 public static DataTable ExecuteQuery(string sql, params SQLiteParameter[] parameters) 156 { 157 using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 158 { 159 using (SQLiteCommand command = new SQLiteCommand()) 160 { 161 PrepareCommand(command, connection, sql, parameters); 162 163 SQLiteDataAdapter adapter = new SQLiteDataAdapter(command); 164 DataTable data = new DataTable(); 165 try { adapter.Fill(data); } 166 catch (Exception) { throw; } 167 return data; 168 } 169 } 170 } 171 172 #endregion 173 174 #region SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] parameters) 執行一個查詢語句,返回一個關聯的SQLiteDataReader實例。 175 /// <summary> 176 /// 執行一個查詢語句,返回一個關聯的SQLiteDataReader實例。 177 /// </summary> 178 /// <param name="sql">要執行的查詢語句。</param> 179 /// <param name="parameters">執行SQL查詢語句所需要的參數,參數必須以它們在SQL語句中的順序為準。</param> 180 /// <returns></returns> 181 /// <exception cref="Exception"></exception> 182 public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] parameters) 183 { 184 using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 185 { 186 using (SQLiteCommand command = new SQLiteCommand()) 187 { 188 try 189 { 190 PrepareCommand(command, connection, sql, parameters); 191 return command.ExecuteReader(CommandBehavior.CloseConnection); 192 } 193 catch (Exception) { throw; } 194 } 195 } 196 } 197 198 #endregion 199 200 #region DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, params SQLiteParameter[] parameters)分頁查詢 201 /// <summary> 202 /// 分頁查詢 203 /// </summary> 204 /// <param name="recordCount">總記錄數</param> 205 /// <param name="pageIndex">頁牽引</param> 206 /// <param name="pageSize">頁大小</param> 207 /// <param name="cmdText">Sql命令文本</param> 208 /// <param name="countText">查詢總記錄數的Sql文本</param> 209 /// <param name="parameters">命令參數</param> 210 /// <returns>DataSet</returns> 211 public static DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, params SQLiteParameter[] parameters) 212 { 213 if (recordCount < 0) 214 recordCount = int.Parse(ExecuteScalar(countText).ToString()); 215 var ds = new DataSet(); 216 using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 217 { 218 using (SQLiteCommand command = new SQLiteCommand()) 219 { 220 PrepareCommand(command, connection, cmdText, parameters); 221 222 SQLiteDataAdapter adapter = new SQLiteDataAdapter(command); 223 adapter.Fill(ds, (pageIndex - 1) * pageSize, pageSize, "result"); 224 } 225 return ds; 226 } 227 } 228 #endregion 229 230 #region DataTable GetSchema()查詢數據庫中的所有數據類型信息 231 /// <summary> 232 /// 查詢數據庫中的所有數據類型信息。 233 /// </summary> 234 /// <returns></returns> 235 /// <exception cref="Exception"></exception> 236 public static DataTable GetSchema() 237 { 238 using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 239 { 240 try 241 { 242 connection.Open(); 243 return connection.GetSchema("TABLES"); 244 } 245 catch (Exception) { throw; } 246 } 247 } 248 #endregion 249 250 #region int ResetDataBass() 重置自動增長列,如果執行了刪除操作,自動增長列就會變的不連續,通過使用VACUUM方式重置 251 /// <summary> 252 /// 重置自動增長列,如果執行了刪除操作,自動增長列就會變的不連續,通過使用VACUUM方式重置 253 /// </summary> 254 public static int ResetDataBass() 255 { 256 using (SQLiteConnection conn = new SQLiteConnection(connectionString)) 257 { 258 using (SQLiteCommand command = new SQLiteCommand()) 259 { 260 PrepareCommand(command, conn, "vacuum"); 261 262 return command.ExecuteNonQuery(); 263 } 264 } 265 } 266 #endregion 267 }
SQLite-C#-幫助類