【问题标题】:Query does not update the table查询不更新表
【发布时间】:2019-09-07 06:45:44
【问题描述】:

我正在使用存储过程来完成更新查询,并且我已将消息框设置为在查询成功执行时显示。当我单击按钮时,会弹出消息框,但表格未更新。我在 SQL Server 中运行相同的查询,它更新了表,但无法通过 c# 更新

我已经在 SQL Server 中尝试过相同的代码,代码运行良好。命令执行时弹出消息框

if (IsUpdate)
            {
                MessageBox.Show("Clicked");
                txtCustomerName.Enabled = false;

                using (SqlConnection con = new SqlConnection(ApplicationSettings.ConnectionString()))
                {
                    using (SqlCommand cmd = new SqlCommand("usp_customer_updateProducts", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Contact", Contact);
                        cmd.Parameters.AddWithValue("@Address", address);
                        cmd.Parameters.AddWithValue("@PAN", PAN);
                        cmd.Parameters.AddWithValue("@VAT", VAT);
                        cmd.Parameters.AddWithValue("@CustomerName", CustomerName);
                        con.Open();

                        try
                        {
                            MessageBox.Show(CustomerName);
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Customer Updated Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                            CustomerRecordForm crf = new CustomerRecordForm();
                            crf.Show();
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        finally
                        {
                            con.Close();
                        }
                    }
                }
            }

这里是存储过程的代码:

ALTER procedure [dbo].[usp_customer_updateProducts]
(
@Contact varchar,
@Address varchar,
@PAN varchar,
@VAT varchar,
@CustomerName varchar
)
as
begin
    update [dbo].[CustomerInfo]
    set [ContactNo]=@Contact,
        [Address]=@Address,
        [PAN]=@PAN,
        [VAT]=@VAT
    where [CustomerID] = (select CustomerID from CustomerInfo where CustomerName=@CustomerName)
end

【问题讨论】:

  • 将其更改为 WHERE [CustomerId] IN ( SELECT CustomerId FROM... ) 而不是 WHERE [CustomerId] = ( SELECT CustomerId FROM ... )
  • 仍然遇到同样的错误
  • 也就是说,您可能需要考虑将存储过程的UPDATE 查询更改为使用INNER JOIN 而不是WHERE IN,或者使用初始查询来设置@customerId 变量,因为这可能更容易理解,并让您指出错误情况,其中有 0 行或多于 1 行匹配`@CustomerName`。
  • 但我在同一张表中有记录,所以为什么要使用内连接
  • 你为什么要查找CustomerID?如果这是您的表的主键,那么您应该将@CustomerID 传递到您的存储过程中。传入@CustomerName 并在执行时进行查找可能会导致两行(或更多)行出现相同的信息。

标签: c# sql-server database


【解决方案1】:

您需要为 varchar 数据类型分配长度,否则它只需要一个字符。 请检查以下查询。

DECLARE @Contact varchar

SET @Contact = 'CONTACT'

SELECT @Contact Contact

您需要检查每个列的数据类型及其长度。 请检查此link

如果您知道客户 ID,请发送客户 ID 而不是客户名称作为参数,这对您和 SQL Server 都有帮助。

请检查下面更新的存储过程。

ALTER procedure [dbo].[usp_customer_updateProducts]
(
    @Contact varchar(100), -- You need to define length
    @Address varchar(2000),
    @PAN varchar(50),
    @VAT varchar(50),
    @CustomerName varchar(50) -- If you know the customer ID then please send customer id instead of customer name.
)
as
begin

    SET NOCOUNT ON; -- Please use SET NOCOUNT ON. It will little help in performance.

    UPDATE [dbo].[CustomerInfo]
    SET [ContactNo] = @Contact,
        [Address] = @Address,
        [PAN] = @PAN,
        [VAT] = @VAT
    WHERE CustomerName = @CustomerName -- You can directly use customer name in where condition.

end

【讨论】:

    【解决方案2】:

    根据您的查询和输入共享,建议进行一项修改,截至目前您将@CustomerName传递给程序,现在您在insert时已经管理没有重复的名称记录插入到您的表格中,否则这绝对是您在生产中遇到的错误。

    现在来到您当前的查询部分,因为您从前端发布了CustomerName,所以根据此名称选择您的id 并将其保存在一个变量中,稍后用于更新您的记录。

    declare @Id int
    set @Id = (select CustomerID from CustomerInfo where CustomerName=@CustomerName)
    
     update [dbo].[CustomerInfo]
        set [ContactNo]=@Contact,
            [Address]=@Address,
            [PAN]=@PAN,
            [VAT]=@VAT
        where [CustomerID] = @id
    
    

    出于调试目的,您可以从过程中返回此 id 并将其保存在您的代码中,以检查是否准确地采用了哪个 id 作为更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多