简体   繁体   English

插入Access数据库时插入错误C#

[英]Insert error C# when inserting into access database

I get an error when trying to insert a record into the database. 尝试将记录插入数据库时​​出现错误。

Here's the error: 这是错误:

在此处输入图片说明

The Code: 编码:

connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "INSERT INTO [Booking] (TableID, BookingName, BookingNumber, BookingDate, PartySize) VALUES (@TableID, @BookingName, @BookingPhoneNumber, @BookingDate, @PartySize)";
            command.Parameters.AddRange(new OleDbParameter[] {
                    new OleDbParameter("@TableID", textBoxResTableID.Text),
                    new OleDbParameter("@BookingName", textBoxResName.Text),
                    new OleDbParameter("@BookingPhoneNumber", textBoxResNum.Text),
                    new OleDbParameter("@BookingDate", DateTime.Today),
                    new OleDbParameter("@PartySize", textBoxResPartySize.Text)
                });

            command.ExecuteNonQuery();
            connection.Close();

The DB: 数据库:

在此处输入图片说明

Looks like TABLEID may be a reserved word according to this . 看起来像TABLEID可以根据是保留字 To get round it enclose it in []. 为了解决这个问题,请将其括在[]中。

I had same problem and solved it adding parameters with following syntax: 我遇到了同样的问题,并使用以下语法解决了添加参数的问题:

 command.Parameters.Add("@UserName", OleDbType.BSTR).Value = username;

EDIT : pasted wrong command first. 编辑 :首先粘贴错误的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM