transakcje SQL .NET

Ottobre 31st, 2007

Description: Przyklad wykozystania transakcji

Link: http://www.codekeep.net/snippets/8ce81713-722a-4899-834d-6c0321a46450.aspx

string DeleteContact(int intContactID)
 {
   // Open the database connection.
   ContactMgmt.Open();
   // Declare a transaction object.
   SqlTransaction transDelete;
   // Create the transaction.
   transDelete = ContactMgmt.BeginTransaction(IsolationLevel.ReadCommitted);     // Create the command to delete from Contacts table.     SqlCommand cmdDelete = new SqlCommand("DELETE FROM Contacts" + "
    WHERE ContactID=" + intContactID.ToString(), ContactMgmt, transDelete);     // Execute the commands
     try
       {
           int intRows;
           // Delete row from Contacts table.
           intRows = cmdDelete.ExecuteNonQuery();
           // Delete Calls for this ContactID.
           cmdDelete.CommandText = "DELETE FROM Calls WHERE " +
           " ContactID=" + intContactID.ToString();             intRows = intRows + cmdDelete.ExecuteNonQuery();
           // Commit the transaction.
            transDelete.Commit();
           // Return success message.
            return intRows.ToString() + " deleted.";
        }
        catch
        {
            // Restore the database state if there was an error.
            transDelete.Rollback();
            // Return error message.
            return "Contact could not be deleted.";
         }
         finally
         {
            // Close the database.
            ContactMgmt.Close();
          }
}

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.