[英]Getting “Syntax error (missing operator) in query expression” in C# trying to INSERT INTO .accdb
So I have the below code, and upon clicking "button3", is supposed to insert the contents of my dataGridView into my database. 所以我有下面的代码,点击“button3”后,我应该将dataGridView的内容插入到我的数据库中。 I am getting the error: 我收到错误:
"Syntax error (missing operator) in query expression ' ' , with the first value of my first column in the single quotes, as if it read the data, but can't tell what to do with it. When I click the "OK" to dismiss the message, and another message pops up and the second value of that same column now in single quotes; and so on, until the last value is reached, and then finally, an error box pops up with: “查询表达式中的语法错误(缺少运算符)' ,单引号中我的第一列的第一个值,就好像它读取数据一样,但不知道如何处理它。当我点击”确定“关闭该消息,弹出另一条消息,同一列的第二个值现在用单引号;依此类推,直到达到最后一个值,然后最后弹出一个错误框:
"Syntax error in INSERT INTO statement" “INSERT INTO语句中的语法错误”
I plan to parameterize the code after I get it working; 我计划在工作之后对代码进行参数化; so no worries about that. 所以不用担心。 :-) :-)
Please help!!!! 请帮忙!!!! All help is appreciated. 所有帮助表示赞赏。
private void button3_Click(object sender, EventArgs e)
{
string ConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Search\\Database.accdb"; //Persist Security Info=True";
for(int i=0; i< dataGridView1.Rows.Count;i++)
{
string StrQuery = "INSERT INTO script_Orders (" + "script, " + "cust_Name) VALUES (" + dataGridView1.Rows[i].Cells["Prescription"].Value + ", " + dataGridView1.Rows[i].Cells["Customer_Name"].Value + ");";
try
{
using (OleDbConnection conn = new OleDbConnection (ConnString))
{
using (OleDbCommand comm = new OleDbCommand(StrQuery, conn))
{
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
尝试这个
string StrQuery = String.Format("INSERT INTO script_Orders(script,cust_Name) VALUES ('{0}','{1}')", dataGridView1.Rows[i].Cells["Prescription"].Value, dataGridView1.Rows[i].Cells["Customer_Name"].Value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.