如何使用存储过程更新选定的行 [英] How Do I Update the selected row using stored procedures

查看:67
本文介绍了如何使用存储过程更新选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#SQL server ..我有一个带有checkBoxColumn的datagridView1

in windows form C# SQL server.. I have datagridView1 with a checkBoxColumn

private void buttonUpdate_Click(object sender, EventArgs e)
        {

            {
                List<DataGridViewRow> selectedRows = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                                                      where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
                                                      select row).ToList();
                if (MessageBox.Show(string.Format("Do you want to Update {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in selectedRows)
                    {
                        using (SqlConnection con = new SqlConnection(ConnectionString))
                        {

                            using (SqlCommand cmd = new SqlCommand("update Customers set Name=@Name,Country=@Country,Mark = (@Mark + 1) WHERE CustomerId = @CustomerId", con))
                            {
                                cmd.CommandType = CommandType.Text;
                                cmd.Parameters.AddWithValue("@CustomerId", row.Cells["CustomerId"].Value);
                                cmd.Parameters.AddWithValue("@Name", row.Cells["Name"].Value);
                                cmd.Parameters.AddWithValue("@Mark", row.Cells["Mark"].Value);
                                con.Open();
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                    }

                    this.BindGrid();
                }

            }

        }



当用户勾选datagridview中的复选框并通过点击提交按钮...存储过程将执行它。然后将更新选定的行。

只是我希望这个查询在存储过程中




when the user Tick the checkbox in datagridview and submit by click button... the Stored Procedures will execute it. then the "Selected row" will be updated.
just I want this query to be in a Stored Procedures

"update Customers set Name=@Name,Mark = (@Mark + 1) WHERE CustomerId = @CustomerId""

推荐答案

您只需要将查询转换为存储过程。



您的存储过程应该看起来像 -

You just need to convert your query to a stored procedure.

Your stored procedure should look something like-
CREATE PROCEDURE [dbo].[UpdateCustomer]
(
  @CustomerId INT,
  @Name VARCHAR(200), --size should be same as that in field definition
  @Mark INT, --datatype should be same as in field definiton
)
AS
BEGIN
  UPDATE Customers 
  SET Name=@Name,Mark = (@Mark + 1) 
  WHERE CustomerId = @CustomerId
END





如果它没有帮助,或者您需要进一步帮助实现这一点,请告诉我:))



If it doesn't help or you need further help implementing this, please let me know :)


这篇关于如何使用存储过程更新选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆