Home
Manage Your Code
Snippet: transakcje SQL .NET (C#)
Title: transakcje SQL .NET Language: C#
Description: Przyklad wykozystania transakcji Views: 105
Author: Piotr Grys Date Added: 10/31/2007
Copy Code  
1string DeleteContact(int intContactID)
2 {    
3   // Open the database connection.
4   ContactMgmt.Open();
5   // Declare a transaction object.    
6   SqlTransaction transDelete;     
7   // Create the transaction.
8   transDelete = ContactMgmt.BeginTransaction(IsolationLevel.ReadCommitted);     // Create the command to delete from Contacts table.     SqlCommand cmdDelete = new SqlCommand("DELETE FROM Contacts" + "    
9    WHERE ContactID=" + intContactID.ToString(), ContactMgmt, transDelete);     // Execute the commands
10     try     
11       {       
12           int intRows;        
13           // Delete row from Contacts table.    
14           intRows = cmdDelete.ExecuteNonQuery();      
15           // Delete Calls for this ContactID.       
16           cmdDelete.CommandText = "DELETE FROM Calls WHERE " + 
17           " ContactID=" + intContactID.ToString();             intRows = intRows + cmdDelete.ExecuteNonQuery();
18           // Commit the transaction. 
19            transDelete.Commit();        
20           // Return success message.   
21            return intRows.ToString() + " deleted.";    
22        }    
23        catch     
24        {         
25            // Restore the database state if there was an error.  
26            transDelete.Rollback();        
27            // Return error message.         
28            return "Contact could not be deleted.";     
29         }
30         finally     
31         {        
32            // Close the database.        
33            ContactMgmt.Close(); 
34          } 
35}