我正在将数据从 DataGridView
复制到名为 test1 的数据库表中,但出现了一个无效操作异常。我该如何解决这个问题?
foreach (DataGridViewRow row in dataGridView2.Rows)
{
string mysqlStatement = "INSERT INTO test1(Paper, Authors, ID, GSCitations)VALUES("+row.Cells[0]+","+row.Cells[1]+","+row.Cells[2]+","+row.Cells[3]+");";
MySqlCommand mysqlCmd = new MySqlCommand(mysqlStatement, connection);
mysqlCmd.ExecuteNonQuery();
}
最佳答案
您需要在 SQL 和 C# 代码中用引号将任何字符串值引起来。我假设该 ID 是数字,因此没有将单元格 2 括在引号中。
string mysqlStatement = "INSERT INTO test1(Paper, Authors, ID, GSCitations) VALUES ('"+row.Cells[0]+"','"+row.Cells[1]+"',"+row.Cells[2]+",'"+row.Cells[3]+"');";
关于mysql - 从数据网格复制数据到数据库表时出现无效操作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15052572/