Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用带有DataTableSQL的回滚事务_C#_Sql_Asp.net_Datatable - Fatal编程技术网

C# 使用带有DataTableSQL的回滚事务

C# 使用带有DataTableSQL的回滚事务,c#,sql,asp.net,datatable,C#,Sql,Asp.net,Datatable,我的项目工作正常,但我的问题是,当第二个SP spInsertCorpPlan中出现错误时,CorporationPlan中没有插入值,而第一个表CorporationContact中插入了值 如何回滚此 请帮忙 for (int row = 0; row < dtContact.Rows.Count; row++) { SqlCommand cmdContact

我的项目工作正常,但我的问题是,当第二个SP spInsertCorpPlan中出现错误时,CorporationPlan中没有插入值,而第一个表CorporationContact中插入了值

如何回滚此

请帮忙

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


您可以使用SqlTransaction来实现这一点。请参见上的示例。但是,请确保您选择了正确的隔离级别,否则可能会导致死锁

基本上,您的代码如下所示:

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