我想删除 datagridview 中选定的行,并删除 Mysql 中的行。
private void deleteOrderButton_Click(object sender, EventArgs e)
{
int selectedIndex = orderDataGridView.CurrentCell.RowIndex;
if (selectedIndex > -1)
{
orderDataGridView.Rows.RemoveAt(selectedIndex);
orderDataGridView.Refresh();
}
string constring = "datasource=localhost;port=3306;username=admin;password=acw123";
string Query = "delete from database.tem_order where temp_orderID = ????
MySqlConnection conDatabase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDatabase);
MySqlDataReader myReader;
conDatabase.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Updated");
我在那里卡住了 MySql 命令; 有人帮忙吗?
最佳答案
在 DataGridView 上使用数据源时,您可以检索所选行的对象。
DataRow row = (dataGridView.SelectedRows[0].DataBoundItem as DataRowView).Row;
DataRow“行”应包含允许您删除 MySQL 中记录的 ID。 将“ID-column-name”替换为真实的列名
using(MySqlConnection sqlConn = new MySqlConnection("datasource=localhost;port=3306;username=admin;password=acw123"))
{
sqlConn.Open();
using(MySqlCommand sqlCommand = new MySqlCommand("DELETE FROM tem_order WHERE temp_orderID = " + row["ID-column-name"],sqlConn))
{
sqlCommand.ExecuteNonQuery();
}
}
关于c# - 删除 datagridview 中选定的行并删除数据库中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976348/