简体   繁体   English

在数据表SQL中使用回滚事务

[英]Using Rollback transaction with datatable sql

My project works properly but my issue is when there's an error occured in the second SP spInsertCorpPlan there is no value inserted in CorporationPlan while there is a value inserted in First table CorporationContact. 我的项目正常运行,但是我的问题是,当第二个SP spInsertCorpPlan中发生错误时,CorporatePlan中没有插入值,而第一个表CorporationContact中却插入了值。

How to Rollback this.? 如何回滚。?

Please help. 请帮忙。

                   for (int row = 0; row < dtContact.Rows.Count; row++)
                    {
                        SqlCommand cmdContact = new SqlCommand("_spInsertCorpContact", _DentalConOpen());
                        cmdContact.CommandType = CommandType.StoredProcedure;
                        cmdContact.Parameters.Add("@CorpCode", SqlDbType.VarChar).Value = Corporation.CorpCode;
                        cmdContact.Parameters.Add("@ContactType", SqlDbType.VarChar).Value = dtContact.Rows[row][0].ToString();
                        cmdContact.Parameters.Add("@AreaCode", SqlDbType.VarChar).Value = dtContact.Rows[row][1].ToString();
                        cmdContact.Parameters.Add("@ContactNo", SqlDbType.VarChar).Value = dtContact.Rows[row][2].ToString();
                        cmdContact.Parameters.Add("@User", SqlDbType.VarChar).Value = Corporation.User;

                        cmdContact.ExecuteNonQuery();
                        cmdContact.Dispose();
                        cmdContact.Connection.Close();
                    }
                    for (int row = 0; row < dtPlan.Rows.Count; row++)
                    {
                        SqlCommand cmdPlan = new SqlCommand("_spInsertCorpPlan", _DentalConOpen());
                        cmdPlan.CommandType = CommandType.StoredProcedure;
                        cmdPlan.Parameters.Add("@CorpCode", SqlDbType.VarChar).Value = Corporation.CorpCode;
                        cmdPlan.Parameters.Add("@PlanID", SqlDbType.VarChar).Value = dtPlan.Rows[row][0].ToString();
                        cmdPlan.Parameters.Add("@EffectiveDate", SqlDbType.DateTime).Value = Convert.ToDateTime(dtPlan.Rows[row][2]);
                        cmdPlan.Parameters.Add("@ExpiryDate", SqlDbType.DateTime).Value = Convert.ToDateTime(dtPlan.Rows[row][3]);
                        cmdPlan.Parameters.Add("@User", SqlDbType.VarChar).Value = Corporation.User;

                        cmdPlan.ExecuteNonQuery();
                        cmdPlan.Dispose();
                        cmdPlan.Connection.Close();
                    }

My Stored Procedures: 我的存储过程:

ALTER PROCEDURE [dbo].[_spInsertCorpContact]
    @CorpCode varchar(20),
    @ContactType varchar(1),
    @AreaCode varchar(10),
    @ContactNo varchar(20),
    @User varchar (50)

AS
BEGIN

    Insert into CorporationContact 
    (CorpCode,
    ContactType,
    AreaCode,
    ContactNo,
    CreateBy,
    CreateDate,
    UpdateBy,
    UpdateDate)

    values
    (@CorpCode,
    @ContactType,
    @AreaCode,
    @ContactNo,
    @User,
    GETDATE(),
    '',
    null
    )
END

ALTER PROCEDURE [dbo].[_spInsertCorpPlan]
    @CorpCode varchar(20),
    @PlanID varchar(20),
    @EffectiveDate DATETIME,
    @ExpiryDate DATETIME,
    @User varchar (50)

AS
BEGIN

    Insert into CorporationPlan
    (CorporationPlanID,
    CorpCode,
    PlanCode,
    EffectiveDate,
    ExpiryDate,
    CreateBy,
    CreateDate,
    UpdateBy,
    UpdateDate)

    values
    (@CorpCode+@PlanID,
    @CorpCode,
    @PlanID,
    @EffectiveDate,
    @ExpiryDate,
    @User,
    GETDATE(),
    '',
    null
    )
END

You can use SqlTransaction to achieve this. 您可以使用SqlTransaction实现此目的。 See the sample on this MSDN page . 请参阅此MSDN页面上的示例。 However, make sure you the right isolation level otherwise you might end up creating deadlocks. 但是,请确保您设置了正确的隔离级别,否则最终可能会造成死锁。

Essentially, your code would look something like this: 本质上,您的代码将如下所示:

using (var connection = new SqlConnection(connectionString))
{
    connection.Open();
    var transaction = connection.BeginTransaction();

    try
    { 
         // Initialize and execute the first command
         SqlCommand command = GetFirstCommand();
         command.Connection = connection;
         command.ExecuteNonQuery();

         // Initialize and execute the first command
         command = GetSecondCommand();
         command.Connection = connection;
         command.ExecuteNonQuery();

         transaction.Commit();
    }
    catch (Exception ex)
    {
         transaction.Rollback();
         throw;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM