So I have the below code, and upon clicking "button3", is supposed to insert the contents of my dataGridView into my database. 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"
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.