简体   繁体   English

将数据从datagridview保存到Mysql时出现c#错误

[英]c# error on saving data from datagridview to Mysql

I have a problem on saving an items in the datagridview together with their orderno on the mysqldatabase, I've tried to use this codes: 我在datagridview中将项目与mysqldatabase上的orderno一起保存时遇到问题,我试图使用这些代码:

 connection = new MySqlConnection(ConnectionString);
        connection.Open();
        command = new MySqlCommand(sql, connection);
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {

            sql = "insert into Item values('" + torderno.Text + "','" + dataGridView1.Rows[i].Cells["itemno"].Value + "');";
            command = new MySqlCommand(sql, connection);
            command.ExecuteNonQuery();
            //Column count doesn't match value count at row 1

        }
        connection.Close();

after I run it their is an error appeared which is this one: "Column count doesn't match value count at row 1" 在我运行它之后,它出现了一个错误,就是这个:“列数与第1行的值计数不匹配”

what should I do? 我该怎么办? do you have an idea whats the problem of this? 你知道这个问题吗?

Take a look at your table and see how many columns are there, in you insert query the number of values must equal the columns or you should write columns name (except auto incremental column) such as following: 看看你的表,看看有多少列,在你插入查询中,值的数量必须等于列,或者你应该写列名称(自动增量列除外),如下所示:

insert into tblName(colName1,colName2,...) values (val1,val2,...);

or 要么

insert into Item VALUES (vlueForCol1,valueForCol2,valueForCol3,...);

in other words your table has more columns than you are inserting into. 换句话说,您的表有比您插入的列更多的列。

take a look at here 看看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM