事务控制:form上添加一个Label1和button,将下面代码加入button的click事件。

string myConnString ="Jet OLEDB:Database Password=;Data Source=\"C:\\db1.mdb\";Password=;Provider=\"Microsoft.Jet.OLEDB.4.0\";User ID=Admin";
   OleDbConnection myConnection = new OleDbConnection(myConnString);
   myConnection.Open();

   OleDbCommand myCommand = myConnection.CreateCommand();
   OleDbTransaction myTrans;

   myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted);
   myCommand.Connection = myConnection;
   myCommand.Transaction = myTrans;
   try
   {
    myCommand.CommandText = "Insert into test (name, address) VALUES ('Liu', 'Japan')";
    myCommand.ExecuteNonQuery();
    myCommand.CommandText = "Insert into test (name, address) VALUES ('Mark',              Germany')";
    myCommand.ExecuteNonQuery();
    myTrans.Commit();
    Console.WriteLine("Both records are written to database.");
   }
   catch(Exception ex)
   {
    try
    {
     myTrans.Rollback();
    }
    catch (OleDbException oex)
    {
     if (myTrans.Connection != null)
     {
      this.Label1.Text = this.Label1.Text + oex.GetType() ;
     }
    }
  
    this.Label1.Text =  this.Label1.Text + ex.GetType();
    this.Label1.Text = this.Label1.Text+"Neither record was written to database";
   }
   finally
   {
    myConnection.Close();
   }
点击后即可在test表中加入记录