SQLite本地事务处理
private void toolStripButton1_Click(object sender, EventArgs e) { //判断新增的年度是否已经存在 if (HasYear()) { MessageBox.Show("该年度分配给各单位的任务已经存在!"); return; } else { #region 方法总结 //var q = from p in mf.DS.UnitPaperTask.AsEnumerable() // where p.year == tscombYear.SelectedItem.ToString() // select p; //foreach (var i in q) //{ //这个命令是直接插入库中,系统由于IO问题会非常慢,有假死现象 // mf.unitpapertaskTap.Insert(i.unitid, i.paperid, i.plantnum, tscombNewYear.SelectedItem.ToString().Trim()); //} //mf.unitpapertaskTap.Update(mf.DS.UnitPaperTask); //mf.unitpapertaskTap.Dispose(); //mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask); // //解决办法,在内存中把表筛选后,复制一个datatable,循环改year值的同时“在内存中”加入新行值。最后提交一次提交到库 //通过实践,updata也是一行一行的增加到数据库,同样的慢,看来上面的方法也对。 #endregion #region 解决方法二也是不行 //string oldYear = tscombYear.SelectedItem.ToString(); //string newYear = tscombNewYear.SelectedItem.ToString(); //DataTable dt = mf.DS.UnitPaperTask.Select("year='" + oldYear + "'").CopyToDataTable(); ////dataGridView1.DataSource = dt; //foreach (DataRow dr in dt.Rows) //{ // dr["year"] = newYear; // //内存中加入行 可用 dataGridView1.DataSource = mf.DS.UnitPaperTas;显示出来 // mf.DS.UnitPaperTask.AddUnitPaperTaskRow(dr["unitid"].ToString(), dr["paperid"].ToString(), Convert.ToInt32(dr["plantnum"].ToString()), dr["year"].ToString()); //} ////dataGridView1.DataSource = mf.DS.UnitPaperTask; ////也是到数据库了,同样的很慢。 //mf.unitpapertaskTap.Update(mf.DS.UnitPaperTask); //删除再填充是为了取得ID //mf.unitpapertaskTap.Dispose(); //mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask); #endregion #region 解决方法三采用事务处理 string oldYear = tscombYear.SelectedItem.ToString(); string newYear = tscombNewYear.SelectedItem.ToString(); string datasource = ConfigurationManager.ConnectionStrings["DBzd.Properties.Settings.baokanConnectionString"].ConnectionString.ToString(); var qUnitTask = from p in mf.DS.UnitPaperTask.AsEnumerable() where p.year == oldYear select p; var qComnyTask = from p in mf.DS.UnitCompMoney.AsEnumerable() where p.year == oldYear select p; //加入了详细的任务列表 using (SQLiteConnection conn = new SQLiteConnection(datasource)) { conn.Open(); using (System.Data.SQLite.SQLiteTransaction trans = conn.BeginTransaction()) { using (SQLiteCommand cmd = new SQLiteCommand(conn)) { cmd.Transaction = trans; try { foreach (var i in qUnitTask) { cmd.CommandText = @"INSERT INTO UnitPaperTask(unitid,paperid,plantnum,year) VALUES('" + i.unitid+ "','" +i.paperid + "','" + i.plantnum + "','" + newYear + "')"; cmd.ExecuteNonQuery(); } trans.Commit(); } catch (Exception ex) { MessageBox.Show(ex.Message); trans.Rollback(); } } } } //需要加入企业任务和指定金额的任务单位 using (SQLiteConnection con = new SQLiteConnection(datasource)) { con.Open(); using (SQLiteTransaction tran = con.BeginTransaction()) { using (SQLiteCommand cm = new SQLiteCommand(con)) { cm.Transaction = tran; try { foreach (var i in qComnyTask) { cm.CommandText = @"insert into UnitCompMoney(unitid,compprices,year) values ('" + i.unitid + "','" + i.compprices + "','" + newYear + "')"; cm.ExecuteNonQuery(); } tran.Commit(); } catch (Exception ex) { MessageBox.Show(ex.Message); tran.Rollback(); } } } } #endregion #region 第四种方法。用SQLiteHelper实现--最后是实再不了的,因为参数只能传递一次,不能随着循环而改变值 //string oldYear = tscombYear.SelectedItem.ToString(); //string newYear = tscombNewYear.SelectedItem.ToString(); //DataTable dt = mf.DS.UnitPaperTask.Select("year='" + oldYear + "'").CopyToDataTable(); //SQLiteParameter[] Paras = new SQLiteParameter[] { // new SQLiteParameter("@unitid",dt.Rows[0][1].ToString()), // new SQLiteParameter("@paperid",dt.Rows[0][2].ToString()), // new SQLiteParameter("@plantnum",dt.Rows[0][3].ToString()), // new SQLiteParameter("@year",newYear) //}; //string sql = "insert into UnitPaperTask(unitid,paperid,plantnum,year) VALUES ( @unitid,@paperid,@plantnum,@year)"; //int rs = SQLiteHelper.TransExecuteNonQuery(dt, sql, Paras); //MessageBox.Show("增加新年度任务成功:"+rs.ToString()); #endregion #region 第5种方法 因为第3种是本地事处理,虽然很好的实现功能,现在想把两个表的操作放在一个分布式事务中。--这样不行SQLite没有 CommittableTransaction #endregion mf.unitpapertaskTap.Dispose(); mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask); mf.unittaskmoeyTap.Dispose(); mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask); AddToolYear(); } }