c# 操作Sqlite批量插入数据
本身sqlite没有提供批量插入的机制,所以,我们需要通过事务处理,首先构件下面方法:
1: /// <summary>
2: /// 执行多条SQL语句,实现数据库事务。
3: /// </summary>
4: /// <param name="SQLStringList">多条SQL语句</param>
5: public static void ExecuteSqlTran(ArrayList SQLStringList)
6: {
7: using (SQLiteConnection conn = new SQLiteConnection(connectionString))
8: {
9: conn.Open();
10: SQLiteCommand cmd = new SQLiteCommand();
11: cmd.Connection = conn;
12: SQLiteTransaction tx = conn.BeginTransaction();
13: cmd.Transaction = tx;
14: try
15: {
16: for (int n = 0; n < SQLStringList.Count; n++)
17: {
18: string strsql = SQLStringList[n].ToString();
19: if (strsql.Trim().Length > 1)
20: {
21: cmd.CommandText = strsql;
22: cmd.ExecuteNonQuery();
23: }
24: }
25: tx.Commit();
26: }
27: catch (System.Data.SQLite.SQLiteException E)
28: {
29: tx.Rollback();
30: throw new Exception(E.Message);
31: }
32: }
33: }
在批量数据的时候,需要首先构件ArrayList的集合;然后调用上述方法!