C# 确保所有更新都已成功完成?

C# 确保所有更新都已成功完成?,c#,asp.net,.net,sql-server,sql-server-2008,C#,Asp.net,.net,Sql Server,Sql Server 2008,我有一些存储过程,当提交新表单时,它们会对一些表进行各种更新和插入。它们都是从我的C应用程序调用的 现在所有的东西都是try-catch格式的,有没有一种方法可以确保在将更改提交到数据库之前,它们都成功地完成了 让我们假设前3个存储过程一切正常,但第4个存储过程失败,我想逆转前3个存储过程中已经完成的操作 全有或全无类型的交易。我不确定您是如何配置的;但是您显然可以使用SqlException类。但另一种方法可能是: int result = command.ExecuteNonQuery();

我有一些存储过程,当提交新表单时,它们会对一些表进行各种更新和插入。它们都是从我的C应用程序调用的

现在所有的东西都是try-catch格式的,有没有一种方法可以确保在将更改提交到数据库之前,它们都成功地完成了

让我们假设前3个存储过程一切正常,但第4个存储过程失败,我想逆转前3个存储过程中已经完成的操作


全有或全无类型的交易。

我不确定您是如何配置的;但是您显然可以使用SqlException类。但另一种方法可能是:

int result = command.ExecuteNonQuery();
if(result == 1)
{
    // Successfully Entered A Row
}
else
{
    // Insert Row Failed
}
这是一种可能的测试方法。本质上,它是在测试查询,如果它返回一行,那么它就成功了,如果它不返回,那么它就会失败。我不确定它是否符合你的标准,但这是你可以测试的两种方法

更新:

因为我不能阅读,但我相信你想要实现一种交易形式。这将实际处理所有请求,如果失败,将回滚更改。它确实会增加很多开销,在某些情况下还会导致其他性能问题。因此,您需要根据需要定制和优化数据库吞吐量

这里有一些信息


希望这能有所帮助。

我不确定您是如何配置的;但是您显然可以使用SqlException类。但另一种方法可能是:

int result = command.ExecuteNonQuery();
if(result == 1)
{
    // Successfully Entered A Row
}
else
{
    // Insert Row Failed
}
这是一种可能的测试方法。本质上,它是在测试查询,如果它返回一行,那么它就成功了,如果它不返回,那么它就会失败。我不确定它是否符合你的标准,但这是你可以测试的两种方法

更新:

因为我不能阅读,但我相信你想要实现一种交易形式。这将实际处理所有请求,如果失败,将回滚更改。它确实会增加很多开销,在某些情况下还会导致其他性能问题。因此,您需要根据需要定制和优化数据库吞吐量

这里有一些信息


希望这会有所帮助。

我会捕获您在前3个中添加的新项目的唯一键,如果您失败,只需根据这些键删除。

我会捕获您在前3个中添加的新项目的唯一键,如果您失败,只需根据这些键删除。

您应该阅读本文

还有这个

你应该看看这个

还有这个

您需要使用System.Transactions.TransactionScope类

//assuming Table1 has a single INT column, Column1 and has one row with value 12345
//and connectionstring contains a valid connection string to the database.
    //this automatically starts a transaction for you
try
{
            using (TransactionScope ts = new TransactionScope())
            {
        //you can open as many connections as you like within the scope although they need to be on the same server. And the transaction scope goes out of scope if control leaves this code.
               using (SqlConnection conn = new SqlConnection(connectionstring))
               {
                  conn.Open();
                  using (SqlCommand comm = new SqlCommand("Insert into Table1(Column1) values(999)")
                  {
                    comm.ExecuteNonQuery();
                  }
                   using (SqlCommand comm1 = new SqlCommand("DELETE from Table1 where Column1=12345"))
                   {
                     comm1.ExecuteNonQuery();
                   }
                }//end using conn
               ts.Complete() ; //commit the transaction; Table1 now has 2 rows (12345 and 999) 
            }//end using ts

}
  catch(Exception ex)
   {
     //Transaction is automatically rolled back for you at this point, Table1 retains original row.
   }
您需要使用System.Transactions.TransactionScope类

//assuming Table1 has a single INT column, Column1 and has one row with value 12345
//and connectionstring contains a valid connection string to the database.
    //this automatically starts a transaction for you
try
{
            using (TransactionScope ts = new TransactionScope())
            {
        //you can open as many connections as you like within the scope although they need to be on the same server. And the transaction scope goes out of scope if control leaves this code.
               using (SqlConnection conn = new SqlConnection(connectionstring))
               {
                  conn.Open();
                  using (SqlCommand comm = new SqlCommand("Insert into Table1(Column1) values(999)")
                  {
                    comm.ExecuteNonQuery();
                  }
                   using (SqlCommand comm1 = new SqlCommand("DELETE from Table1 where Column1=12345"))
                   {
                     comm1.ExecuteNonQuery();
                   }
                }//end using conn
               ts.Complete() ; //commit the transaction; Table1 now has 2 rows (12345 and 999) 
            }//end using ts

}
  catch(Exception ex)
   {
     //Transaction is automatically rolled back for you at this point, Table1 retains original row.
   }

@MadamZuZu是的,我相信是的。我在我们的一个应用程序中使用了它,为了优化它,我们不得不进行大量的重构。@MadamZuZu是的,我相信是这样。我在我们的一个应用程序中使用了它,为了优化它,我们不得不进行大量的重构。虽然这可以给出正确的答案,但人们普遍认为,只链接的答案应该以注释的形式给出,或者至少报告一些代码,以避免@Steve-这一点很公平,帖子更新了一些细节+1现在我真的很喜欢它。这是最好的答案,至少对我来说是这样。虽然这可能会给出一个正确的答案,但人们普遍认为只链接的答案应该以评论的形式给出,或者,至少,报告一些代码以避免@Steve-足够公平,发布更新了一些细节+1现在我真的很喜欢它。至少对我来说,这是最好的答案。