更新语句插入一个新行而不是更新

Update statement inserts a new row instead of updating

我的代码应该只更新 table 中的一个特定行。我也使用了一个简单的过程来更新当前应用程序中的记录,并且除了这种情况外它运行没有任何问题。每次查询运行时,它实际上插入一个新行而不是仅仅更新现有的:

这是我的 C# 代码:

        try
        {
            command.Parameters.Clear();
            command.Parameters.AddRange(vars);
            command.CommandText = "Update" + tableName;

            conn.Open();
            command.ExecuteNonQuery();
            conn.Close();

            this.GetData(tableName);
        }
        catch
        {
            throw;
        }

这是我的SQL代码(请忽略'Alter Procedure'声明,我只是想获取程序的核心脚本):

    ALTER procedure [dbo].[UpdateExpenditureItems]
    (@ID int,
    @Name nvarchar(50),
    @IsGroup bit,
    @Parent nvarchar(50),
    @Description nvarchar(50)
    )

    AS

    Begin

--a new inserted row seems to be a result of running IF statement rather than ELSE
    if @Parent != null
        begin
        declare @ParentID int

        set @ParentID = (select ID from ExpenditureItems where Name=@Parent);

        UPDATE ExpenditureItems SET Name =@Name, Parent =@ParentID, [Description] =@Description, IsGroup = @IsGroup WHERE ID=@ID;

        end
    else
        begin
        UPDATE ExpenditureItems SET Name =@Name, [Description] =@Description WHERE ID=@ID
        end
    end

由于信誉限制,我无法post截图... 提前致谢。

 @Parent != null

这不是您在 SQL

中检查 null 的方式

改为:

 @Parent is not null

原因是当前表单是从另一个表单继承的,当我按下 OK 按钮时,基础 class 的 OK 按钮点击事件首先被触发,然后是子表单。

解决方案:要么以 运行 基本 class 按钮单击事件结束,要么使用虚拟和覆盖来覆盖基本 class 方法。