gpt4 book ai didi

c# - ExecuteNonQuery 要求命令有事务

转载 作者:太空狗 更新时间:2023-10-29 18:16:41 33 4
gpt4 key购买 nike

当我尝试执行以下代码时收到此错误消息。

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction

谁能告诉我问题出在哪里?我想问题的根源是我尝试执行存储过程的部分。

存储过程在执行时创建自己的事务

 using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();

SqlCommand command = conn.CreateCommand();
SqlTransaction transaction;

// Start a local transaction.
transaction = conn.BeginTransaction("createOrder");

// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = conn;
command.Transaction = transaction;

try
{
command.CommandText = "INSERT INTO rand_resupply_order (study_id, centre_id, date_created, created_by) " +
"VALUES (@study_id, @centre_id, @date_created, @created_by) SET @order_id = SCOPE_IDENTITY()";

command.Parameters.Add("@study_id", SqlDbType.Int).Value = study_id;
command.Parameters.Add("@centre_id", SqlDbType.Int).Value = centre_id;
command.Parameters.Add("@date_created", SqlDbType.DateTime).Value = DateTime.Now;
command.Parameters.Add("@created_by", SqlDbType.VarChar).Value = username;

SqlParameter order_id = new SqlParameter("@order_id", SqlDbType.Int);
//study_name.Value =
order_id.Direction = ParameterDirection.Output;
command.Parameters.Add(order_id);

command.ExecuteNonQuery();
command.Parameters.Clear();

//loop resupply list
for (int i = 0; i < resupplyList.Count(); i++)
{
try
{
SqlCommand cmd = new SqlCommand("CreateOrder", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@study_id", SqlDbType.Int).Value = study_id;
cmd.Parameters.Add("@centre_id", SqlDbType.Int).Value = centre_id;
cmd.Parameters.Add("@created_by", SqlDbType.VarChar).Value = username;
cmd.Parameters.Add("@quantity", SqlDbType.VarChar).Value = resupplyList[i].Quantity;
cmd.Parameters.Add("@centre_id", SqlDbType.Int).Value = centre_id;
cmd.Parameters.Add("@depot_id", SqlDbType.VarChar).Value = depot_id;
cmd.Parameters.Add("@treatment_code", SqlDbType.Int).Value = centre_id;
cmd.Parameters.Add("@order_id", SqlDbType.Int).Value = (int)order_id.Value;
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
transaction.Rollback();
ExceptionUtility.LogException(ex, "error");
throw ex;
}
catch (Exception ex)
{
transaction.Rollback();
ExceptionUtility.LogException(ex, "error");
throw ex;
}
finally
{
conn.Close();
conn.Dispose();
}

}

return (int)order_id.Value;

}
catch (Exception ex)
{
transaction.Rollback();
ExceptionUtility.LogException(ex, "error");
throw ex;
}
finally
{
// Attempt to commit the transaction.
transaction.Commit();

conn.Close();
conn.Dispose();
command.Dispose();
}

最佳答案

在使用事务时,你应该在任何地方都使用它。

    cmd.Transaction = transaction;

关于c# - ExecuteNonQuery 要求命令有事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8830637/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com