当前位置: 首页>>代码示例>>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;未经允许,请勿转载。