如何使用MySQL查询将组合框的所有项目发送到SQL DB

时间:2015-10-21 17:39:36

标签: c# mysql

这些是我想要发送到数据库的值

enter image description here

private void radButton1_Click(object sender, EventArgs e)
{
    string constring = "datasource=localhost;port=3306;username=root;";
    string Query = "insert into rhms.reservation(no_table)values('" + this.comboBox1.SelectAll() + "');";

    MySqlConnection condatabase = new MySqlConnection(constring);
    MySqlCommand cmd = new MySqlCommand(Query, condatabase);
    MySqlDataReader myread;
    try
    {
        condatabase.Open();
        myread = cmd.ExecuteReader();
        MessageBox.Show("saved");
        this.Refresh();
        while (myread.Read())
        {
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在查询字符串中设置正确的信息。现在它不正确。数据必须用逗号分隔:

string Query = "insert into rhms.reservation(no_table) values " + this.comboBox1.Items
.Select(p=>string.Format("({0})",p.ToString()))
.Aggregate((p1,p2)=>p1 + "," + p2) + ";"