使用 SqlTransaction 提交多个 SqlCommand

本文关键字:SqlCommand 提交 SqlTransaction 使用 | 更新日期: 2023-09-27 17:56:47

我正在尝试将SqlCommand列表传递到保存与数据库连接的成员函数中。

public void CommitAsTransaction(List<SqlCommand> commands) {
    SqlTransaction transaction = null;
    SqlConnection connection = null;
    try {
        connection = this.CreateSqlConnection();
        connection.Open();
        transaction = connection.BeginTransaction("TransactionID");
        foreach (SqlCommand cmd in commands) {
            cmd.Transaction = transaction;
            cmd.Connection = connection;
            cmd.ExecuteNonQuery();
        }
        transaction.Commit();
    }
    catch (Exception ex) {
        transaction.Rollback();
    }
    connection.Close();
}

这就是我目前拥有的。发生此错误的原因是命令似乎正在原地执行,并且从未达到transaction.Commit();。我见过很多人这样做,不确定我做错了什么。

PS:问题是将要执行的存储过程必须在单个事务中全部运行,我不控制这些并且它们是加密的,它们必须在事务中运行的原因是因为它们在具有PK要求的表中创建了临时记录。

使用 SqlTransaction 提交多个 SqlCommand

您可以使用事务范围吗?像这样:

// place this code inside CommitAsTransaction
using (TransactionScope scope = new TransactionScope())
{
     Boolean AllOK = true;
     SqlConnection connection = this.CreateSQLConnection();
     try
     {
         connection.Open()
     }
     catch (Exception e)
     {
       // deal with it how you need to
       AllOK = false;
     }
     if (AllOK)
     {
        foreach(SQlCommand cmd in Commands)
        {
            try
            {
                 cmd.Connection = connection;
                 cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
               // Deal with it.. 
               AllOK = false;
               break;
            }
        }
        if (AllOK)
        {
           scope.Complete();
           try
           {
               connection.Close();
           }
           catch (Exception e)
           {
             // deal with it
           }
        }
    }
}

非常感谢。我最终根据其他人的组合答案自己弄清楚了,因为谢谢这里是我使用的代码:

public List<Models.eConnectModels.eConnStatus> CommitAsTransaction(List<SqlCommand> commands) 
{
    SqlTransaction transaction = null;
    SqlConnection connection = null;
    List<eConnStatus> ErrorList = new List<eConnStatus>();
            
    try 
    {
        connection = this.CreateSqlConnection();
        connection.Open();
                
        transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted, "TransactionID");
        foreach (SqlCommand cmd in commands) 
        {
            eConnStatus curErr = new eConnStatus();
            cmd.Transaction = transaction;
            cmd.Connection = connection;
            
            SqlParameter errorString = cmd.Parameters.Add("@oErrString", SqlDbType.VarChar);
            errorString.Direction = ParameterDirection.Output;
            errorString.Size = 8000;
            SqlParameter errorStatus = cmd.Parameters.Add("@O_iErrorState", SqlDbType.Int);
            errorStatus.Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            curErr.ErrorState = (int)cmd.Parameters["@O_iErrorState"].Value;
            curErr.ErrorMessage = (string)cmd.Parameters["@oErrString"].Value;
            ErrorList.Add(curErr);
        }
        transaction.Commit();
    }
    catch (Exception ex) 
    {
        transaction.Rollback();
        connection.Close();
        throw ex;
    }
    
    connection.Close();
    return ErrorList;
}