使用combobox radiobuttons和按钮c#更新mysql中的数据

时间:2014-03-14 17:46:38

标签: c#

所以我可以选择在组合框中选择数据,我选择它我可以选择单选按钮从1到5我将对它进行评级,当我选择单选按钮并按下按钮时它应该更新mysql中的数据但idk这里有什么问题..

代码:

private void button2_Click(object sender, EventArgs e)
    {
        if(radioButton1.Checked)
        {
            string constring = "datasource=localhost;port=3306;username=root;password=";
            string Query = "UPDATE filmi.film SET '" + this.comboBox1.SelectedItem + "' = 1";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            cmdDataBase.Parameters.AddWithValue("@film", this.comboBox1.SelectedItem);
            MySqlDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                MessageBox.Show("Ocena oddana!");
                while (myReader.Read())
                {
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

1。替换此:

string Query = "UPDATE filmi.film SET '" + this.comboBox1.SelectedItem + "' = 1";

有了这个:

string Query = "UPDATE filmi.film SET film = @film";

2。替换此:

myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Ocena oddana!");
while (myReader.Read())
{
}

有了这个:

cmdDataBase.ExecuteNonQuery();

修改

如果要为每次更新更新或增加列值

试试这个:

string Query = "UPDATE [TableName] SET [COLUMNNAME] = [COLUMNNAME]+1";