none
关于事务 RRS feed

答案

  • 您好,请参考:
    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
     {
            //在这里放修改数据库的代码
    }

    http://msdn.microsoft.com/zh-cn/library/system.transactions.transactionscope.aspx
    2009年8月1日 13:39
  • 你好!
         代码实例如下,希望对你有帮助:
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
    
        // Start a local transaction.
        SqlTransaction sqlTran = connection.BeginTransaction();
    
        // Enlist a command in the current transaction.
        SqlCommand command = connection.CreateCommand();
        command.Transaction = sqlTran;
    
        try
        {
            // Execute two separate commands.
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
            command.ExecuteNonQuery();
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
            command.ExecuteNonQuery();
    
            // Commit the transaction.
            sqlTran.Commit();
            Console.WriteLine("Both records were written to database.");
        }
        catch (Exception ex)
        {
            // Handle the exception if the transaction fails to commit.
            Console.WriteLine(ex.Message);
    
            try
            {
                // Attempt to roll back the transaction.
                sqlTran.Rollback();
            }
            catch (Exception exRollback)
            {
                // Throws an InvalidOperationException if the connection 
                // is closed or the transaction has already been rolled 
                // back on the server.
                Console.WriteLine(exRollback.Message);
            }
        }
    }
    

    周雪峰
    • 已标记为答案 xw2009ds 2009年8月1日 15:42
    2009年8月1日 14:25
    版主

全部回复

  • 您好,请参考:
    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
     {
            //在这里放修改数据库的代码
    }

    http://msdn.microsoft.com/zh-cn/library/system.transactions.transactionscope.aspx
    2009年8月1日 13:39
  • 你好!
         代码实例如下,希望对你有帮助:
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
    
        // Start a local transaction.
        SqlTransaction sqlTran = connection.BeginTransaction();
    
        // Enlist a command in the current transaction.
        SqlCommand command = connection.CreateCommand();
        command.Transaction = sqlTran;
    
        try
        {
            // Execute two separate commands.
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
            command.ExecuteNonQuery();
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
            command.ExecuteNonQuery();
    
            // Commit the transaction.
            sqlTran.Commit();
            Console.WriteLine("Both records were written to database.");
        }
        catch (Exception ex)
        {
            // Handle the exception if the transaction fails to commit.
            Console.WriteLine(ex.Message);
    
            try
            {
                // Attempt to roll back the transaction.
                sqlTran.Rollback();
            }
            catch (Exception exRollback)
            {
                // Throws an InvalidOperationException if the connection 
                // is closed or the transaction has already been rolled 
                // back on the server.
                Console.WriteLine(exRollback.Message);
            }
        }
    }
    

    周雪峰
    • 已标记为答案 xw2009ds 2009年8月1日 15:42
    2009年8月1日 14:25
    版主
  • 谢谢版主的解疑,不过楼上代码的注释全是英文,看不太懂得说~~~~还得麻烦版主将那些注释翻译为中文……
    2009年8月1日 15:43
  • 我帮你翻译一下啊:
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
    
        // 开始一个本地事务
        SqlTransaction sqlTran = connection.BeginTransaction();
    
        // 在当前事务中添加一个命令
        SqlCommand command = connection.CreateCommand();
        command.Transaction = sqlTran;
    
        try
        {
            // 执行两个独立的命令
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
            command.ExecuteNonQuery();
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
            command.ExecuteNonQuery();
    
            // 提交事务
            sqlTran.Commit();
            Console.WriteLine("Both records were written to database.");
        }
        catch (Exception ex)
        {
            // 如果事务提交失败,处理异常
            Console.WriteLine(ex.Message);
    
            try
            {
                // 尝试回滚事务
                sqlTran.Rollback();
            }
            catch (Exception exRollback)
            {
                // 如果连接被关闭或事务已经在服务器上回滚则抛出 InvalidOperationException 异常 
                
                Console.WriteLine(exRollback.Message);
            }
        }
    }
    


    周雪峰
    2009年8月2日 1:05
    版主