Syntax error in INSERT INTO statement. System.Data.OleDb.OleDbErrorCollection(INSERT INTO 语句中的语法错误.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:UsersadminDesktopdelSHAFIdb.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());
}
}
推荐答案
你的代码是正确的,除了缺少 using 语句 围绕一次性对象,但真正的问题是单词 NUMBER.它是 MSAccess 的保留关键字.
用方括号括起来.
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:UsersadminDesktopdelSHAFIdb.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:INSERT INTO 语句中的语法错误.System.Data.OleDb.OleDbE
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01