INSERT INTO 语句中的语法错误.System.Data.OleDb.OleDbE

Syntax error in INSERT INTO statement. System.Data.OleDb.OleDbErrorCollection(INSERT INTO 语句中的语法错误.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)