INSERT INTO 语句中的语法错误.System.Data.OleDb.OleDbErrorCollection [英] Syntax error in INSERT INTO statement. System.Data.OleDb.OleDbErrorCollection

查看:24
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆