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
基础教程推荐
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 将数据集转换为列表 2022-01-01
- 如果条件可以为空 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
