Sure, here’s the translation: 如何在C#中使用ComboBox删除数据库中的数据?

huangapple go评论83阅读模式
英文:

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 SelectCategoryComboBoxBut it does nothing.it acts like an un-programed button.
here is my code:"(我使用以下代码从上面的表中使用名为SelectCategoryComboBoxComboBox来删除数据,但它什么都不做,就像一个未编程的按钮一样。以下是我的代码:)

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 SelectCategoryComboBoxBut 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()

huangapple
  • 本文由 发表于 2020年1月6日 23:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定