INSERT语句中的语法错误 - C#,Access 2013,VS Express 2013

时间:2014-09-07 09:04:40

标签: c# insert oledb ms-access-2013

我一直试图找出为什么我的程序一直给我错误。 system.data.oledb.oledbexception(0x80040E14):INSERT INTO语句中的语法错误。

  • 表名:User
  • 列:

    Username
    AccountNumber
    FirstName
    LastName
    

代码:

namespace Library_System
{
    public partial class CreateAccountWindow : Form
    {
        OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data         Source=C:\Users\Jc\Documents\Visual Studio 2013\Projects\Library System\Library     System\LibrarySystemDatabase.accdb;Persist Security Info=False;");
    OleDbCommand command = new OleDbCommand();
    //OleDbDataReader reader;

    public CreateAccountWindow()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        string Username = "", AccountNumber = "", FirstName = "", LastName = "";
        //int Borrowed = 0;
        bool hasValue1 = false, hasValue2 = false, hasValue3 = false, hasValue4 = false;
        if (textBox1.Text != "")
        {
            label1.Hide();
            Username = textBox1.Text;
            hasValue1 = true;
        }
        else
        {
            label1.Show();
            label1.Text = "Required";
        }

        if (textBox10.Text != "")
        {
            label21.Hide();

            AccountNumber = textBox8.Text;
            hasValue2 = true;
        }
        else
        {
            label21.Show();
            label21.Text = "Required";
        }

        if (textBox8.Text != "")
        {
            label13.Hide();

            FirstName = textBox10.Text;
            hasValue3 = true;
        }
        else
        {
            label13.Show();
            label13.Text = "Required";
        }

        if (textBox7.Text != "")
        {
            label12.Hide();
            label12.Text = "Required";
            LastName = textBox7.Text;
            hasValue4 = true;
        }
        else
        {
            label12.Show();
            label12.Text = "Required";
        }

        if (hasValue1 || hasValue2 || hasValue3 || hasValue4)
        {
            try
            {
                connect.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connect;
                command.CommandText = "insert into User (Username,AccountNumber,FirstName,LastName) values ('" + Username + "','" + AccountNumber + "','" + FirstName + "','" + LastName + "')";
                command.ExecuteNonQuery();
                MessageBox.Show("REGISTRATION COMPLETE !!", "DONE");
                connect.Close();
            }
            catch (Exception ex)
            {
                connect.Close();
                MessageBox.Show("Error:"+ex.ToString());
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您收到错误是因为USER是Access SQL中的保留字,因此您需要将表名括在方括号中。此外,正如对问题的评论中提到的marc_s,您应该使用如下参数化查询:

// test data
string Username = "gord";
string AccountNumber = "gt001";
string FirstName = "Gord";
string LastName = "Thompson";

command.CommandText =
        "INSERT INTO [User] (Username, AccountNumber, FirstName, LastName) VALUES (?,?,?,?)";
command.Parameters.AddWithValue("?", Username);
command.Parameters.AddWithValue("?", AccountNumber);
command.Parameters.AddWithValue("?", FirstName);
command.Parameters.AddWithValue("?", LastName);
command.ExecuteNonQuery();

答案 1 :(得分:0)

您需要一个UNIQUE字段作为主键。 Fields that generate numbers automatically in Access

SelectCommand = "select @@IDENTITY";
var id = (int)SelectCommand.ExecuteScalar();

答案 2 :(得分:0)

您是否尝试直接在db控制台上运行插入查询?在你的情况下它的访问。你可能有一些拼写错误。 你已经初始化了oledbcommand对象两次。你可能不需要在oledbconnection对象之后初始化它。

关于你可以使用验证器的方法;点击此链接How to: Validate Data

对于oledbcommand参数检查以下链接:

OleDBParams

Update Data using OLEDB

Insert data using OLEDB