英文:
How can I Delete Data in a Database Using ComboBox in C#?
问题
以下是我从您提供的内容中翻译好的部分:
"Here is a Picture of My Table Named CategoryTable
"(这是我命名为CategoryTable
的表的图片)。
"I used The Following Code to Delete Data from the Above Table using a ComboBox
which is named as SelectCategoryComboBox
But it does nothing.it acts like an un-programed button.
here is my code:"(我使用以下代码从上面的表中使用名为SelectCategoryComboBox
的ComboBox
来删除数据,但它什么都不做,就像一个未编程的按钮一样。以下是我的代码:)
try
{
String conString = ConfigurationManager.ConnectionStrings["mfcdb"].ConnectionString;
String query = "DELETE FROM CategoryTable WHERE CategoryName='" + SelectCategoryComboBox.SelectedText + "'";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
}
}
catch
{
MessageBox.Show("Error");
}
请注意,我已经保留了代码的原始格式。
英文:
Here is a Picture of My Table Named CategoryTable
I used The Following Code to Delete Data from the Above Table using a ComboBox
which is named as SelectCategoryComboBox
But it does nothing.it acts like an un-programed button.
here is my code:
try
{
String conString = ConfigurationManager.ConnectionStrings["mfcdb"].ConnectionString;
String query = "DELETE FROM CategoryTable WHERE CategoryName='" + SelectCategoryComboBox.SelectedText + "'";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
}
}
catch{
MessageBox.Show("Error");
}
答案1
得分: 0
改变
SelectCategoryComboBox.SelectedText
为
SelectCategoryComboBox.SelectedItem.ToString()
英文:
Change
SelectCategoryComboBox.SelectedText
to
SelectCategoryComboBox.SelectedItem.ToString()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论