当存储的产品之一在.net c# 中发现失败时数据回滚

发布于 2024-11-08 11:00:16 字数 1281 浏览 3 评论 0原文

有谁知道如果存储过程之一在此过程中失败,如何在 .net c# 中回滚所有数据?

示例:

 protected void btnSave_Click(object sender, EventArgs e)
   {
      int ixTest= SaveTest("123");
      CreateTest(ixTest);

   }

   protected int SaveTest(int ixTestID)
   {
      SubSonic.StoredProcedure sp = Db.SPs.TestInsert(
         null,
         ixTestID);
      sp.Execute();
      int ixTest= (int)sp.OutputValues[0];
      return ixTest;
   }


   private long CreateTest(int ixTest)
   {
      long ixTestCustomer = CreateTestCustomer();

      TestCustomer testCustomer= new TestCustomer();
      try
      {
          testCustomer.TestCustomerId = ixTest;
          testCustomer.InteractionId = ixTestCustomer ;
          testCustomer.Save();
      }
      catch
      {
          Response.Redirect("pgCallSaveFailure.aspx");
      }

      m_saleDetail = new TestSaleDetail();
      try
      {
          m_saleDetail.SaleId = sale.SaleId;
          m_saleDetail.Save();
      }
      catch
      {
          Response.Redirect("pgCallSaveFailure.aspx");
      }

      return ixTestCustomer ;
   }

我有以下代码将调用 btnSave_Click,然后它将调用另外 2 个函数 Savetest() 和 CreateTest() 将数据保存到数据库中。如果问题仅发生在 CreateTest() 中并且 Savetest() 已成功运行,如何回滚以下代码中的所有数据事务。如何回滚 Savetest() 和 CreateTest() 的所有数据?

Does anyone know how can I all data rollback in .net c# if one of the stored proc failed during the process?

Example:

 protected void btnSave_Click(object sender, EventArgs e)
   {
      int ixTest= SaveTest("123");
      CreateTest(ixTest);

   }

   protected int SaveTest(int ixTestID)
   {
      SubSonic.StoredProcedure sp = Db.SPs.TestInsert(
         null,
         ixTestID);
      sp.Execute();
      int ixTest= (int)sp.OutputValues[0];
      return ixTest;
   }


   private long CreateTest(int ixTest)
   {
      long ixTestCustomer = CreateTestCustomer();

      TestCustomer testCustomer= new TestCustomer();
      try
      {
          testCustomer.TestCustomerId = ixTest;
          testCustomer.InteractionId = ixTestCustomer ;
          testCustomer.Save();
      }
      catch
      {
          Response.Redirect("pgCallSaveFailure.aspx");
      }

      m_saleDetail = new TestSaleDetail();
      try
      {
          m_saleDetail.SaleId = sale.SaleId;
          m_saleDetail.Save();
      }
      catch
      {
          Response.Redirect("pgCallSaveFailure.aspx");
      }

      return ixTestCustomer ;
   }

I have the following code will call to btnSave_Click, then it will call to another 2 function Savetest() and CreateTest() to save data into the database. How can I rollback all data transaction in the following code if issue only happened in CreateTest() and which Savetest() have run successfully. How can I rollback all data for both Savetest() and CreateTest()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

献世佛 2024-11-15 11:00:16

代码执行准确如下。希望你能明白。

public void SaveData()
{
SqlConnection connDB = new SqlConnection();
SqlCommand cmdExecuting = new SqlCommand();


try {
    connDB = new SqlConnection(connection_string);
    cmdExecuting.Connection = connDB;
    connDB.Open();
    cmdExecuting.Transaction = connDB.BeginTransaction();


    int result = 0;

    result = Method1(cmdExecuting);

    if (result != 0) {
        cmdExecuting.Transaction.Rollback();
        return;
    }

    result = Method2(cmdExecuting);

    if (result != 0) {
        cmdExecuting.Transaction.Rollback();
        return;
    }

    cmdExecuting.Transaction.Commit();

} catch (Exception ex) {
    cmdExecuting.Transaction.Rollback();
} finally {
    cmdExecuting.Dispose();
    cmdExecuting = null;
    connDB.Close();
    connDB = null;
}


}

public int Method1(SqlCommand cmdExecuting)
{

cmdExecuting.Parameters.Clear();
cmdExecuting.CommandText = "stored proc 01";
cmdExecuting.CommandType = CommandType.StoredProcedure;

cmdExecuting.Parameters.Add("@para1", SqlDbType.Int);
cmdExecuting.Parameters("@para1").Value = value;

return cmdExecuting.ExecuteScalar();

}


public int Method2(SqlCommand cmdExecuting)
{

cmdExecuting.Parameters.Clear();
cmdExecuting.CommandText = "stored proc 02";
cmdExecuting.CommandType = CommandType.StoredProcedure;

cmdExecuting.Parameters.Add("@para1", SqlDbType.Int);
cmdExecuting.Parameters("@para1").Value = value;

return cmdExecuting.ExecuteScalar();

}

Code performs exact is given below. Hope you can get the idea.

public void SaveData()
{
SqlConnection connDB = new SqlConnection();
SqlCommand cmdExecuting = new SqlCommand();


try {
    connDB = new SqlConnection(connection_string);
    cmdExecuting.Connection = connDB;
    connDB.Open();
    cmdExecuting.Transaction = connDB.BeginTransaction();


    int result = 0;

    result = Method1(cmdExecuting);

    if (result != 0) {
        cmdExecuting.Transaction.Rollback();
        return;
    }

    result = Method2(cmdExecuting);

    if (result != 0) {
        cmdExecuting.Transaction.Rollback();
        return;
    }

    cmdExecuting.Transaction.Commit();

} catch (Exception ex) {
    cmdExecuting.Transaction.Rollback();
} finally {
    cmdExecuting.Dispose();
    cmdExecuting = null;
    connDB.Close();
    connDB = null;
}


}

public int Method1(SqlCommand cmdExecuting)
{

cmdExecuting.Parameters.Clear();
cmdExecuting.CommandText = "stored proc 01";
cmdExecuting.CommandType = CommandType.StoredProcedure;

cmdExecuting.Parameters.Add("@para1", SqlDbType.Int);
cmdExecuting.Parameters("@para1").Value = value;

return cmdExecuting.ExecuteScalar();

}


public int Method2(SqlCommand cmdExecuting)
{

cmdExecuting.Parameters.Clear();
cmdExecuting.CommandText = "stored proc 02";
cmdExecuting.CommandType = CommandType.StoredProcedure;

cmdExecuting.Parameters.Add("@para1", SqlDbType.Int);
cmdExecuting.Parameters("@para1").Value = value;

return cmdExecuting.ExecuteScalar();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文