简体   繁体   中英

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.

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. See the sample on this MSDN page . 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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