简体   繁体   English

为什么此存储过程不更新数据库中的数据?

[英]Why is this stored procedure not updating data in a database?

I am running a stored procedure with a hardcoded value. 我正在运行具有硬编码值的存储过程。 The command executes successfully without any errors. 该命令成功执行,没有任何错误。 But data is not updating in the database. 但是数据库中的数据没有更新。 If I run that stored procedure with SQL Server, data is updating. 如果我使用SQL Server运行该存储过程,则数据正在更新。 What's my mistake? 我怎么了

C# code C#代码

using (SqlTransaction sqlTrans = con.BeginTransaction())
{
    using (SqlCommand AdjustTax = new SqlCommand("GP_SOP_AdjustTax", con, sqlTrans))
    {
        try
        {
            AdjustTax.CommandType = CommandType.StoredProcedure;
            AdjustTax.Parameters.Add("@IN_SOPType", SqlDbType.Int).Value = 3;
            AdjustTax.Parameters.Add("@IN_SOPNo", SqlDbType.VarChar).Value = "stdinv2278";
            AdjustTax.Parameters.Add("@IN_AdjustAmount", SqlDbType.Int).Value = 0.04;
            AdjustTax.Parameters.Add("@O_iError", SqlDbType.Int, 250);
            AdjustTax.Parameters["@O_iError"].Direction = ParameterDirection.Output;

            if (con == null || con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            AdjustTax.ExecuteNonQuery();
            int Error = (int)AdjustTax.Parameters["@O_iError"].Value;

            if (Error == 0)
            {
                MessageBox.Show("Tax is Adjusted");
            }
            if (Error != 0)
            {
                MessageBox.Show("Error No:" + Error1);
            }

            sqlTrans.Commit();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            sqlTrans.Rollback();
        }

        finally
        {
            con.Close();
        }
    }
}

SQL Server code SQL Server代码

DECLARE @return_value int,
        @O_iError int

EXEC    @return_value = [dbo].[GP_SOP_AdjustTax]    
        @IN_SOPType = 3,   
        @IN_SOPNo = 'stdinv2278',    
        @IN_AdjustAmount = 0.04,    
        @O_iError = @O_iError OUTPUT

SELECT  @O_iError as N'@O_iError'

SELECT  'Return Value' = @return_value

GO

I think the problem is being caused by this line: 我认为问题是由以下原因引起的:

AdjustTax.Parameters.Add("@IN_AdjustAmount", SqlDbType.Int).Value = 0.04;

Since the type is SqlDbType.Int and you are passing in 0.04, that value is most likely getting rounded to 0. Without seeing the contents of the actual stored procedure, I can only guess that passing in a value of 0 for that parameter either causes the stored procedure to skip the update, or the calculation that the stored procedure does results in the column being updated to the same value it originally had. 由于类型为SqlDbType.Int而您传入的是0.04,因此该值很可能会舍入为0。在未看到实际存储过程的内容的情况下,我只能猜测为该参数传入值0会导致存储过程以跳过更新,或者计算该存储过程确实会导致该列被更新为与原始值相同的值。

I would try changing that line to: 我会尝试将该行更改为:

AdjustTax.Parameters.Add("@IN_AdjustAmount", SqlDbType.Decimal).Value = 0.04M

EDIT 编辑
Decimal is the proper mapping for the Numeric Sql Type, as explained here . DecimalNumeric Sql类型的正确映射,如此处所述

尝试SqlDbType.Decimal:

AdjustTax.Parameters.Add("@IN_AdjustAmount", SqlDbType.Decimal).Value = 0.04;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM