本文整理汇总了C#中System.Data.SqlClient.SqlTransaction.Rollback方法的典型用法代码示例。如果您正苦于以下问题:C# SqlTransaction.Rollback方法的具体用法?C# SqlTransaction.Rollback怎么用?C# SqlTransaction.Rollback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlTransaction
的用法示例。
在下文中一共展示了SqlTransaction.Rollback方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteSqlTransaction
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();
// Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
示例2: Main
//引入命名空间
using System;
using System.Data;
using System.Data.SqlClient;
class Savepoint {
public static void Main() {
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);
mySqlConnection.Open();
SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction();
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.Transaction = mySqlTransaction;
mySqlCommand.CommandText =
"INSERT INTO Customers ( " +
" CustomerID, CompanyName " +
") VALUES ( " +
" 'J8COM', 'J8 Company' " +
")";
int numberOfRows = mySqlCommand.ExecuteNonQuery();
Console.WriteLine("Number of rows inserted = " + numberOfRows);
mySqlTransaction.Save("SaveCustomer");
mySqlCommand.CommandText = "INSERT INTO Orders (CustomerID ) VALUES ( 'J8COM' )";
numberOfRows = mySqlCommand.ExecuteNonQuery();
Console.WriteLine("Number of rows inserted = " + numberOfRows);
mySqlTransaction.Rollback("SaveCustomer");
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName " +
"FROM Customers " +
"WHERE CustomerID = 'J8COM'";
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
while (mySqlDataReader.Read()) {
Console.WriteLine("mySqlDataReader[\" CustomerID\"] = " +
mySqlDataReader["CustomerID"]);
Console.WriteLine("mySqlDataReader[\" CompanyName\"] = " +
mySqlDataReader["CompanyName"]);
}
mySqlDataReader.Close();
mySqlCommand.CommandText = "DELETE FROM Customers WHERE CustomerID = 'J8COM'";
numberOfRows = mySqlCommand.ExecuteNonQuery();
Console.WriteLine("Number of rows deleted = " + numberOfRows);
mySqlTransaction.Commit();
mySqlConnection.Close();
}
}