c# – ExecuteNonQuery要求命令进行事务处理

c# – ExecuteNonQuery要求命令进行事务处理,第1张

概述我尝试执行以下代码时收到此错误消息. ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction 任何人都可以建议问题出在哪里?我想问题的根源是我尝试执行存储过程的部分. 存储过程在执行 我尝试执行以下代码时收到此错误消息.
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要求命令进行事务处理全部内容,希望文章能够帮你解决c# – ExecuteNonQuery要求命令进行事务处理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1240574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存