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(); 

        } 

    } 

} 

 

你可能感兴趣的:(sqlite)