/// <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 ();
}
}