SQLite批量插入,修改数据库 zt
SQLiteConnection sqConnection = dataProvider.GetDbConnection(); sqConnection.Open(); SQLiteCommand sqCommand = new SQLiteCommand(); SQLiteTransaction myTrans; // Start a local transaction myTrans = sqConnection.BeginTransaction(System.Data.IsolationLevel.Serializable); // Assign transaction object for a pending local transaction sqCommand.Connection = sqConnection; sqCommand.Transaction = myTrans; try { for (int i = 0; i < n; i++) { sqCommand.CommandText = "Insert into Dept(DeptNo, DName)Values(@DeptNo, @DName)"; sqCommand.Parameters.Add(new SQLiteParameter("@DeptNo", i)); sqCommand.Parameters.Add(new SQLiteParameter("@DName",”DEVELOPMENT”)); sqCommand.ExecuteNonQuery(); } myTrans.Commit(); } catch (Exception e) { myTrans.Rollback(); Error = String.Format("Neither record was written to database,Details:{0}", e.ToString()); } finally { sqConnection.Close(); }
public static void RunSQLiteTransaction(string myConnString) { using (SQLiteConnection sqConnection = new SQLiteConnection(myConnString)) { sqConnection.Open(); // Start a local transaction SQLiteTransaction myTrans = sqConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); SQLiteCommand sqCommand = sqConnection.CreateCommand(); try { sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, ‘DEVELOPMENT‘)"; sqCommand.ExecuteNonQuery(); sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, ‘PRODUCTION‘)"; sqCommand.ExecuteNonQuery(); myTrans.Commit(); Console.WriteLine("Both records are written to database."); } catch (Exception e) { myTrans.Rollback(); Console.WriteLine(e.ToString()); Console.WriteLine("Neither record was written to database."); } finally { sqCommand.Dispose(); myTrans.Dispose(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。