[mysql]删除按钮 SQL 数据库 Windows 窗体应用程序

发布时间: 2017/1/2 14:45:31
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

如何修改我的删除按钮代码来从我的 SQL 数据库中删除选定的行?目前当我选择一行,然后单击删除按钮删除的所有行。

private void delete_button1_Click_1(object sender, EventArgs e)
    {
        if (dataGridView1.SelectedRows.Count > 0)
        {
            int selectedIndex = dataGridView1.SelectedRows[0].Index;
            string sqlquery;
            string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            MySqlConnection con = new MySqlConnection(ConString);
            con.Open();
            int rowID = int.Parse(dataGridView1[0, selectedIndex].Value.ToString());
            sqlquery = "DELETE FROM hotel_booking WHERE BookingID = BookingID";

            try
            {
                MySqlCommand command = new MySqlCommand(sqlquery, con);
                command.ExecuteNonQuery();
                string CmdString = "SELECT * FROM hotel_booking";
                MySqlDataAdapter sda = new MySqlDataAdapter(CmdString, con);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

解决方法 1:

不尝试运行此查询,它将删除所有行

DELETE FROM hotel_booking WHERE BookingID = BookingID

BookingID = BookingID意味着它永远是真实

我认为你正在使用

int rowID = int.Parse(dataGridView1[0, selectedIndex].Value.ToString());
sqlquery = "DELETE FROM hotel_booking WHERE BookingID = "+ rowID;
官方微信
官方QQ群
31647020