SQLite local transaction processing

Source: Internet
Author: User
Tags sqlite

 Private voidToolstripbutton1_click (Objectsender, EventArgs e) {            //determine if the new year already exists            if(Hasyear ()) {MessageBox.Show ("the tasks assigned to units in the year already exist! "); return; }            Else            {                #regionMethod Summary//var q = from P in MF. Ds. Unitpapertask.asenumerable ()//where p.year = = tscombYear.SelectedItem.ToString ()//select P; //foreach (var i in Q)//{                //This command is inserted directly into the library, the system due to the IO problem will be very slow, there is the phenomenon of suspended animation//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); //                //workaround, after filtering the table in memory, copy a DataTable and cycle to the year value and add the new row value "in memory". Submit to library last commit//through practice, Updata is also a row of the increase to the database, the same slow, it seems that the above method is also true.                 #endregion                #regionSolution two, too, no way.//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; //    //the in-memory join line is available Datagridview1.datasource = MF. Ds. Unitpapertas; show it.//MF. Ds. Unitpapertask.addunitpapertaskrow (dr["Unitid"). ToString (), dr["Paperid"]. ToString (), Convert.ToInt32 (dr["Plantnum"]. ToString ()), dr["year"].                ToString ()); //}                ////datagridview1.datasource = MF. Ds.                Unitpapertask; ////also to the database, the same is very slow.                 //mf.unitpapertaskTap.Update (MF. Ds.                Unitpapertask); //Remove and repopulate is to get ID//Mf.unitpapertaskTap.Dispose (); //Mf.unitpapertaskTap.Fill (MF. Ds. Unitpapertask);                #endregion                #regionSolution three uses transaction processingstringOldyear =tscombYear.SelectedItem.ToString (); stringNewYear =tscombNewYear.SelectedItem.ToString (); stringDataSource = configurationmanager.connectionstrings["DBzd.Properties.Settings.baokanConnectionString"].                Connectionstring.tostring (); varQunittask = fromPinchMf. Ds. Unitpapertask.asenumerable ()whereP.year = = OldyearSelectp; varQcomnytask = fromPinchMf. Ds. Unitcompmoney.asenumerable ()whereP.year = = OldyearSelectp; //added a detailed list of tasks                using(Sqliteconnection conn =Newsqliteconnection (DataSource)) {Conn.                    Open (); using(System.Data.SQLite.SQLiteTransaction trans =Conn. BeginTransaction ()) {using(Sqlitecommand cmd =NewSqlitecommand (conn)) {cmd. Transaction=Trans; Try                            {                                foreach(varIinchqunittask) {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 (); }                        }                    }                }                //task units that need to join enterprise tasks and specify amounts                using(Sqliteconnection con =Newsqliteconnection (DataSource)) {con.                    Open (); using(Sqlitetransaction tran =con. BeginTransaction ()) {using(Sqlitecommand cm =NewSqlitecommand (Con)) {cm. Transaction=Tran; Try                            {                                foreach(varIinchqcomnytask) {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                #regionFourth method. Implemented with Sqlitehelper--the last is no more, because parameters can only be passed once and cannot change values with loops//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 ("Add New Year Mission success:" +rs. ToString ());                #endregion                #regionThe 5th method because the 3rd is a local thing, although a good implementation of the function, now want to put two table operations in a distributed transaction. --that doesn't work. SQLite has no committabletransaction#endregionMf.unitpapertaskTap.Dispose (); Mf.unitpapertaskTap.Fill (MF. Ds.                Unitpapertask);                Mf.unittaskmoeyTap.Dispose (); Mf.unitpapertaskTap.Fill (MF. Ds.                Unitpapertask);            Addtoolyear (); }        }

SQLite local transaction processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.