INSERT INTO语句中的语法错误. System.Data.OleDb.OleDbErrorCollection [英] Syntax error in INSERT INTO statement. System.Data.OleDb.OleDbErrorCollection
问题描述
我正在Visual Studio 2012 Express中创建基本表单.我正在使用Microsoft Access 2012作为数据库.我的问题是,当我按下提交"按钮时,我什么也没做.
Hi I am creating basic form in Visual Studio 2012 Express. I am using Microsoft Access 2012 as database. My problem is when I press submit button I nothing happens.
INSERT INTO语句中的语法错误. System.Data.OleDb.OleDbErrorCollection
Syntax error in INSERT INTO statement. System.Data.OleDb.OleDbErrorCollection
我的代码如下.请帮助我解决此问题.
My code is given below. Please help me to resolve this issue.
protected void Button1_Click(object sender, EventArgs e)
{
string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\Desktop\del\SHAFI\db.accdb";
OleDbConnection con = new OleDbConnection(conString);
OleDbCommand cmd = con.CreateCommand();
string text = "INSERT INTO TEST (Number, Amount) VALUES (?, ?)";
cmd.CommandText = text;
try
{
con.Open();
cmd.Parameters.AddWithValue("@Number", txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", txtOrder.Text);
cmd.ExecuteNonQuery();
}
catch (OleDbException ex)
{
txtAmount.Text = "Sorry";
Response.Write(ex.Message.ToString() + "<br />" + ex.Errors.GetType());
}
}
推荐答案
除了缺少保留的关键字.
用方括号括起来.
Your code is correct, apart from the lacking of using statement around disposable objects, but the real problem is the word NUMBER. It is a reserved keyword for MSAccess.
Use it enclosed in square brackets.
string text = "INSERT INTO TEST ([Number], Amount) VALUES (?, ?)";
但是,如果仍然可行,我真的建议您更改该列名称.这个问题会变得很烦人.
However, if it is still possible, I really suggest you to change that column name. This problem will become very annoying.
protected void Button1_Click(object sender, EventArgs e)
{
string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\Desktop\del\SHAFI\db.accdb";
using(OleDbConnection con = new OleDbConnection(conString))
using(OleDbCommand cmd = con.CreateCommand())
{
string text = "INSERT INTO TEST ([Number], Amount) VALUES (?, ?)";
cmd.CommandText = text;
try
{
con.Open();
cmd.Parameters.AddWithValue("@Number", txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", txtOrder.Text);
cmd.ExecuteNonQuery();
}
catch (OleDbException ex)
{
txtAmount.Text = "Sorry";
Response.Write(ex.Message.ToString() + "<br />" + ex.Errors.GetType());
}
}
这篇关于INSERT INTO语句中的语法错误. System.Data.OleDb.OleDbErrorCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!