mysql - 从数据网格复制数据到数据库表时出现无效操作异常

标签 mysql c#-4.0 datagridview invalidoperationexception

我正在将数据从 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/

相关文章:

mysql - 我可以用纯 mysql 解决这个问题吗? (加入列中的 ';' 分隔值)

php - 删除密码而不是重置密码

html - 如何在经典的 ASP Web 应用程序之上集成 WebSockets?

c# - 比较两个函数的负载/性能

vb.net - 通知用户正在填充 DataGridView

mysql,创建动态自定义别名

javascript - 更改表中选定的选项

.net - 如何在 .NET 中使用 GetMethod 区分通用签名和非通用签名?

C# DataGridView 检查是否为空

c# - DatagridView 选择最后一行