尝试保存到数据库时,列数与第1行的值计数不匹配

时间:2019-06-03 14:28:55

标签: c# mysql

我正在尝试将数据保存在mysql db上,但是我总是得到“列数与第1行的值计数不匹配”

我试图重新编码此sql命令,看了一些与此有关的视频,但找不到任何有效的方法

string Insert = 
    "INSERT INTO db.pcs(Nome,Marca,Utilizador,Ram,CPU,Disco,TipoSO,SO,Licenca,TipoPC,ProgramaseLicenca) VALUES('" 
            + nome + "," + marca + "," + user + "," + ram + "," 
            + cpu + "," + disco + "," + tiposo + "," + so + "," 
            + lice + "," + tipopc + "," + progs + "')";

using (MySqlConnection cn = new MySqlConnection(connst))
{
    cn.Open();
    MySqlCommand cmd = new MySqlCommand(Insert, cn);

    try
    {
        if (cmd.ExecuteNonQuery() == 1)
        {
            pb1.Visible = true;
            timer1.Start();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

它很容易添加到数据库,但是在try消息框中,我得到了“ Column count doesn't match value count at row 1

2 个答案:

答案 0 :(得分:0)

我认为您应该为每个值加上“”

string Insert = 
"INSERT INTO  db.pcs (Nome,Marca,Utilizador,Ram,CPU,Disco,TipoSO,SO,Licenca,TipoPC,ProgramaseLicenca) VALUES('" 
        + nome + "','" + marca + "','" + user + "','" + ram + "','" 
        + cpu + "','" + disco + "','" + tiposo + "','" + so + "','" 
        + lice + "','" + tipopc + "','" + progs + "')";

答案 1 :(得分:0)

您只能在开头设置一个引号,在结尾设置一个。 因此,这只是您在插入命令中设置的一个值。

尝试此操作或使用sqlparams

            string Insert =
            "INSERT INTO db.pcs(Nome,Marca,Utilizador,Ram,CPU,Disco,TipoSO,SO,Licenca,TipoPC,ProgramaseLicenca) VALUES('"
                    + nome + "', '" + marca + "', '" + user + "', '" + ram + "', '"
                    + cpu + "', '" + disco + "', '" + tiposo + "', '" + so + "', '"
                    + lice + "', '" + tipopc + "', '" + progs + "')";