简体   繁体   English

在C#中尝试INSERT INTO .accdb获取“查询表达式中的语法错误(缺少运算符)”

[英]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.

相关问题 在C#中的查询表达式中获取语法错误(缺少运算符) - Getting a syntax error (missing operator) in query expression in C# 查询表达式中的C#语法错误(缺少运算符) - C# Syntax error (missing operator) in query expression C#查询表达式中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression in C# 查询表达式c#中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression c# 在 C# 中为 MS Access 查询表达式中出现语法错误(缺少运算符) - Getting a syntax error (missing operator) in query expression in C# for MS Access 查询表达式Oledb INSERT语句中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression, Oledb INSERT Statement 获取异常错误说:查询表达式中的语法错误(缺少运算符) - Getting Exception error saying: Syntax error (missing operator) in query expression C# 中的 SQL 查询(System.Data.OleDb.OleDbException:&#39;查询表达式中的语法错误(缺少运算符)) - SQL query in C# (System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression) 无法弄清楚如何在C#中的查询表达式错误中修复语法错误(缺少运算符) - Cannot figure out how to fix syntax error (missing operator) in query expression error in C# 查询表达式 &#39;05-04-2014 AM 12:00:00&#39; C# 中的语法错误(缺少运算符)? - Syntax error (missing operator) in query expression '05-04-2014 AM 12:00:00' C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM