【问题标题】:Update Command to update multiple rows in database from ComboBox Selected Value更新命令以从 ComboBox 选定值更新数据库中的多行
【发布时间】:2020-09-07 15:30:43
【问题描述】:

我知道有很多关于使用SQL adapterDatagrid 更新数据库中的多行的信息,但是在尝试了许多不同的方法后我似乎找不到解决方案。

我有一个SelectionChange 事件,它使用在DataGrid 中选择的当前行填充comboxBox,当dropdown 展开时,它会显示剩余的项目。

我还有一个button_click 事件,我试图让用户从DataGrid 中选择所有必要的行,然后单击button 以使用Combobox 中的SelectedValue 更新这些行

这是我的ButtonClick 事件,我已经能够更新选定的行但无法实现更新多行。如果需要,我可以提供我的SelectionChange 事件和DataGrid 的代码:

    private void butn_Assign_Click(object sender, RoutedEventArgs e)
    {

        try
        {
            SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
            connection.Open();

                int x;

                // Set the UPDATE command and parameters.
                sAdapter.UpdateCommand = new SqlCommand("UPDATE [hb_Disputes] SET ASSGNTO=@ASSGNTO WHERE DSP_ID=@DSP_ID;", connection);
                sAdapter.UpdateCommand.Parameters.Add("@DSP_ID", SqlDbType.Int, 500).Value = txt_ID.Text;
                sAdapter.UpdateCommand.Parameters.Add("@ASSGNTO", SqlDbType.NVarChar, 10).Value = cmb_AnalystName.SelectedValue;
                sAdapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.Both;

                // Execute the update.
                x = sAdapter.UpdateCommand.ExecuteNonQuery();

                if (x >= 1)
                {
                    MessageBox.Show("Dispute has been assigned");
                }


                connection.Close();

                //Update User's High Bill Dispute Count
                Window parentWindow = Window.GetWindow(this);
                ((MainWindow)parentWindow).HBD_Count();


                // Clear Search Fields
                cmb_AnalystName.SelectedIndex = -1;


                //Refresh DataGrid
                AssignList();

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

    }

【问题讨论】:

    标签: c# sql-server wpf datagrid updatecommand


    【解决方案1】:

    为此创建新的存储过程并从您的代码中调用此过程

    像这样传递输入@DSP_ID_CommaSeperated='1,2,4,5,6,8' , @ASSGNTO='20'

    -- 使用拆分函数的第一种方式

    create proc Proc_Name
    @DSP_ID_CommaSeperated varchar(max),
    @ASSGNTO varchar(20)
    as
    begin
    
    
       UPDATE [hb_Disputes] SET ASSGNTO=@ASSGNTO WHERE DSP_ID in
     (
        select items from dbo.split(@DSP_ID_CommaSeperated )
      )
    
    end
    

    -- 使用动态查询的第二种方式

    create proc Proc_Name
    @DSP_ID_CommaSeperated varchar(max),
    @ASSGNTO varchar(20)
    as
    begin
    
    
    declare @Query nvarchar(max)='UPDATE [hb_Disputes] SET ASSGNTO=@ASSGNTO WHERE DSP_ID in('+@DSP_ID_CommaSeperated+')'
    
    exec @Query
    
    end
    

    【讨论】:

    • 这会更新表中的所有行吗?我只想更新用户从DataGrid 中选择的行
    • 这条语句只更新@DSP_ID_CommaSeperated中的记录,这个字符串是逗号分隔的,这不会更新数据库中的所有记录
    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    相关资源
    最近更新 更多