使用带有数据表sql的Rollback事务

时间:2015-01-07 02:21:12

标签: c# sql asp.net datatable

我的项目工作正常但我的问题是当第二个SP spInsertCorpPlan发生错误时,在第一个表公司联系人中插入了值时,在公司计划中没有插入任何值。

如何回滚。?

请帮忙。

                   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();
                    }

我的存储过程:

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

1 个答案:

答案 0 :(得分:0)

您可以使用SqlTransaction来实现此目的。请参阅this MSDN page上的示例。但是,请确保您具有正确的隔离级别,否则可能最终会造成死锁。

基本上,您的代码看起来像这样:

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;
    }
}