Use transaction to group operations (VB.net) : Transaction « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
   'declare connection
   dim Conn as new OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))

   sub Page_Load(Sender as Object, e as EventArgs)
      dim objTrans as OleDbTransaction
      dim objCmd as OleDbCommand = new OleDbCommand _
         ("DELETE from employee where UserID = 32", Conn)

      Conn.Open()
      objTrans = Conn.BeginTransaction()
      objCmd.Transaction = objTrans

          try

         objCmd.CommandText = "INSERT INTO employee " & _
            "(ID, FirstName, LastName) VALUES " & _
            "(109, 'J', 'S')"
         objCmd.ExecuteNonQuery()

         objCmd.CommandText = "INSERT INTO employee " & _
            "(ID, FirstName, LastName) VALUES " & _
            "(119, 'J', 'S')"
         objCmd.ExecuteNonQuery()

         objTrans.Commit()
             Label1.Text = "Both operations performed successfully"
      catch ex as OleDbException
         objTrans.RollBack()
         Label1.Text = ex.Message & ""
         Label1.Text = "Both operations failed"
      finally
         objCmd.Connection.Close()
      end try
   end sub
</script>

<html><body>
   <form runat="server">
      <asp:Label id="Label1" runat="server"
         maintainstate=false /><br>
   </form>
</body></html>








18.57.Transaction
18.57.1.Use transaction to group operations (VB.net)
18.57.2.Execute database commands within a single local transaction. You can test rollback and partial rollback.