ExecuteNonQuery不更新输出参数

时间:2019-04-26 12:53:12

标签: c# sql-server

我正在使用ExecuteNonQuery在数据库中插入一条记录,并返回新记录的ID。可以插入,但输出参数不会更新。

试图在SSMS中执行存储过程,并且工作正常。使用ExecuteNonQuery执行时只有一个问题。

包括所有用于尝试插入的代码。

        public int InsertPurchaseOrder(IPurchaseOrder po)
        {
            int returnId = 0;
            int returnCount = 0;

            List<IParam> parms = ParameterFactory.CreateListInstance();

            parms.Add(ParameterFactory.CreateParameter("@Timestamp", po.Timestamp, SqlDbType.Timestamp, ParameterDirection.Output));
            parms.Add(ParameterFactory.CreateParameter("@Status", po.Status, SqlDbType.Int, ParameterDirection.Input));
            parms.Add(ParameterFactory.CreateParameter("@Subtotal", po.Subtotal, SqlDbType.Money, ParameterDirection.Input));
            parms.Add(ParameterFactory.CreateParameter("@Taxes", po.Taxes, SqlDbType.Money, ParameterDirection.Input));
            parms.Add(ParameterFactory.CreateParameter("@Total", po.Total, SqlDbType.Money, ParameterDirection.Input));
            parms.Add(ParameterFactory.CreateParameter("@EmployeeId", po.EmployeeId, 8, SqlDbType.VarChar, ParameterDirection.Input));

            parms.Add(ParameterFactory.CreateParameter("@PONumOut", 12345, SqlDbType.Int, ParameterDirection.Output));

            try
            {
                returnCount = dataAccess.ExecuteNonQuery(SPROC_INSERT_PO, CommandType.StoredProcedure, parms);
                if (returnCount >= 0)
                {
                    po.Timestamp = (byte[])parms[0].Value;
                    returnId = Convert.ToInt32(parms[6].Value);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returnId;
        }
 public static IParam CreateParameter(string name, object value, SqlDbType type, ParameterDirection direction)
        {
            return new Param(name, value, type, direction);
        }
        public Param(string name, object value, SqlDbType type, ParameterDirection direction)
        {
            Name = name;
            Value = value;
            Size = 0;
            DataType = type;
            Direction = direction;
        }
        public int ExecuteNonQuery(string cmdText, CommandType cmdType, List<IParam> parms)
        {
            SqlCommand cmd = CreateCommand(cmdText, cmdType, parms);

            using (cmd.Connection)
            {
                cmd.Connection.Open();
                return cmd.ExecuteNonQuery();
            }
        }
        public SqlCommand CreateCommand(string cmdText, CommandType cmdType, List<IParam> parms)
        {
            SqlConnection conn = new SqlConnection(
                Properties.Settings.Default.cnnString);

            SqlCommand cmd = new SqlCommand(cmdText, conn);
            cmd.CommandType = cmdType;

            if (parms != null)
            {
                foreach (IParam p in parms)
                {
                    cmd.Parameters.Add(new SqlParameter(p.Name,
                        p.DataType, p.Size));
                    cmd.Parameters[p.Name].Value = p.Value;
                    cmd.Parameters[p.Name].Direction = p.Direction;
                }
            }

            return cmd;
        }
ALTER PROCEDURE [dbo].[spInsertPO]
    @Timestamp timestamp OUTPUT,
    @Status Int,
    @Subtotal money,
    @Taxes money,
    @Total money,
    @EmployeeId varchar(8),
    @PONumOut Int OUTPUT
AS
BEGIN
    BEGIN TRY
        SET NOCOUNT OFF
        INSERT INTO
            PurchaseOrder(Status,Subtotal,Taxes,Total,EmployeeId)
        VALUES
            (@Status, @Subtotal, @Taxes, @Total, @EmployeeId)

        SET @PONumOut = @@IDENTITY;
    END TRY
    BEGIN CATCH
        THROW
    END CATCH
END

2 个答案:

答案 0 :(得分:0)

解决了这个问题。

需要将我的ExecuteNonQuery()方法修改为以下内容。

        public int ExecuteNonQuery(string cmdText, CommandType cmdType, List<IParam> parms)
        {
            SqlCommand cmd = CreateCommand(cmdText, cmdType, parms);

            using (cmd.Connection)
            {
                cmd.Connection.Open();
                int ret = cmd.ExecuteNonQuery();
                UnloadParms(parms, cmd);
                return ret;
            }
        }

并添加以下方法。

private static void UnloadParms(List<IParam> parms, SqlCommand cmd)
        {
            if (parms != null)
            {
                for (int i = 0; i <= parms.Count - 1; i++)
                {
                    IParam p = parms[i];
                    p.Value = cmd.Parameters[i].Value;
                    parms[i] = p;
                }
            }
        }

答案 1 :(得分:0)

请这样使用

`ALTER PROCEEDURE [dbo]。[spInsertPO]     @Timestamp时间戳输出,     @Status Int,     @小计,     @税收,     @总钱,     @EmployeeId varchar(8),     @PONumOut Int输出 如 开始     开始尝试         设置NOCOUNT OFF         插入             PurchaseOrder(状态,小计,税金,总计,员工编号)         价值             (@ Status,@ Subtotal,@ Taxes,@ Total,@ EmployeeId)

    SET @PONumOut = @@IDENTITY;
END TRY
BEGIN CATCH
    THROW
END CATCH

返回@PONumOut;`

END