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();
            }
        }
相關文章
相關標籤/搜索