C # SQL Server transaction rollback

Source: Internet
Author: User

/// <Summary>
/// SQL Server transaction sample class to demonstrate SQL transaction operations
/// By Camus)
/// </Summary>
Public class sqlservertransactiondemo
{
Private sqlservertransactiondemo (){}

/// <Summary>
/// Obtain the SQL Server transaction sample instance method
/// </Summary>
/// <Returns> SQL Server transaction sample instance </returns>
Public static sqlservertransactiondemo gethandle ()
{
Return new sqlservertransactiondemo ();
}

# Region run the SQL Server transaction run Method
/// <Summary>
/// Run SQL Server transactions
/// </Summary>
Public void run ()
{
// Access the Microsoft SQL Server Sample Database northwind. Assume that the SA password of Microsoft SQL Server is empty.
String
Connectionstring = @ "Server = (local); database = northwind; uid = sa; Pwd =; persist Security info = false ;";

// Create a connection object
System. Data. sqlclient. sqlconnection = NULL;
System. Data. sqlclient. sqltransaction = NULL;
Try
{
Sqlconnection = new system. Data. sqlclient. sqlconnection (connectionstring );
Sqlconnection. open (); // open connection

// Start local transaction, connection. begintransaction ()
// Isolationlevel. readcommitted:
// Keep the shared lock when reading data to avoid dirty reading, but you can change the data before the transaction ends, resulting in non-repeated reading or phantom data.
Sqltransaction = sqlconnection. begintransaction (system. Data. isolationlevel. readcommitted, "sqltransaction ");

// Create a command object
System. Data. sqlclient. sqlcommand = sqlconnection. createcommand ();
// Assign the connection and transaction objects to the command object
Sqlcommand. Connection = sqlconnection;
Sqlcommand. Transaction = sqltransaction;

// Start to execute the transaction, which consists of command 1 2 3.
// Execute database command 1
Sqlcommand. commandtext = "insert into region (regionid, regiondescription) values (200, \ 'description \')";
Sqlcommand. executenonquery ();
// Execute database command 2
Sqlcommand. commandtext = "insert into region (regionid, regiondescription) values (201, \ 'description \')";
Sqlcommand. executenonquery ();
// Execute DATABASE Command 3 !!! Similar to command 1, an error occurs.
Sqlcommand. commandtext = "insert into region (regionid, regiondescription) values (200, \ 'description \')";
Sqlcommand. executenonquery ();
// Submit the transaction
Sqltransaction. Commit ();
Console. writeline ("Two database commands have been executed successfully .");
}
Catch (exception E)
{
Try
{
If (sqltransaction! = NULL)
{
// Roll back the transaction
Sqltransaction. rollback ("sqltransaction ");
}
}
Catch (system. Data. sqlclient. sqlexception ex)
{
// Transaction rollback failed
If (sqltransaction. connection! = NULL)
{
Console. writeline ("when performing rollback transaction" + ex. GetType () + "violation! "+ Ex. Message );
}
}

Console. writeline ("when executing database commands," + E. GetType () + "violation! "+ E. Message );
Console. writeline ("Two database commands are not completed .");
}
Finally
{
// Close connection
If (sqlconnection! = NULL)
{
Sqlconnection. Close ();
}
}
}
# Endregion

Public static void main (string [] ARGs)
{
Sqlservertransactiondemo. gethandle (). Run ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.