當前位置: 首頁>>代碼示例>>C#>>正文


C# SqlTransaction.Rollback方法代碼示例

本文整理匯總了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);
            }
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Data.SqlClient,代碼行數:51,代碼來源:SqlTransaction.Rollback

示例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();
    }
}
開發者ID:C#程序員,項目名稱:System.Data.SqlClient,代碼行數:55,代碼來源:SqlTransaction.Rollback


注:本文中的System.Data.SqlClient.SqlTransaction.Rollback方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。