【发布时间】:2016-03-29 06:07:12
【问题描述】:
我正在尝试将记录插入到 sql 数据库中,下面是我通过单击按钮进行插入的代码。
我无法插入记录,并且当我执行代码时它一直抛出错误.....我知道代码中有问题,但我不确定问题出在哪里。 ....
错误信息是“','.. 附近的语法不正确”
private void ADD_button_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection(sqlconn))
{
con.Open();
for (int i = 1; i < dataGridView.Rows.Count; i++)
{
string sql = @"INSERT INTO ERSBusinessLogic VALUES ("
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_ID"].Value + ", "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_Formula"].Value + ", "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsCount"].Value + ", "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_Inputs"].Value + ");";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
finally
{
con.Close();
}
【问题讨论】:
-
显示错误信息,它是什么意思
-
注意查询中所有输入的数据库表的数据类型:+ dataGridView.Rows[i].Cells["ERSBusinessLogic_ID"].Value + ", " + dataGridView.Rows[i].Cells ["ERSBusinessLogic_Formula"].Value + ", " + dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsCount"].Value + ", " + dataGridView.Rows[i].Cells["ERSBusinessLogic_Inputs"].Value + ", " ");";它们必须匹配
-
请检查我的编辑
-
能否展示一下数据库表的结构?在数据库中的每个字符串字段周围,您需要在插入语句中添加一个“
'”。 -
@diiN_(嗨......所以你的意思是如果数据类型是 varchar(例如:ERSbusinesslogic_inputs 是 varchar)......那么代码应该是“'”+ dataGridView.Rows[i]。单元格["ERSBusinessLogic_Inputs"].Value + " ' "
标签: c# .net winforms windows-applications