将记录添加到Access数据库时“INSERT INTO语句中的语法错误”

时间:2022-06-01 20:16:49

I've searched for hours for a solution to this problem but nothing I've read has helped. I'm getting this error when trying to add this record to an Access database. The file I'm trying to save into is named Cats.accdb, with a table named Cats.

我已经搜索了几个小时来解决这个问题,但我读过的内容并没有帮助。尝试将此记录添加到Access数据库时,我收到此错误。我正在尝试保存的文件名为Cats.accdb,其中包含一个名为Cats的表。

Table column names:
CatId (type: text)
CatName (text)
Hair (text)
Size (text)
CareType (text)
Notes (text)
AdoptDate (date/time general date), Weight (double), Age (integer) (I've commented any reference to these columns out in the C# code to attempt to debug with just plain old text boxes. At first I thought it was because of something to do with using a DateTimePicker, but it still throws the error after commenting out.)

表列名:CatId(类型:文本)CatName(文本)头发(文本)大小(文本)CareType(文本)注释(文本)AdoptDate(日期/时间一般日期),重量(双倍),年龄(整数)(I已经在C#代码中对这些列的任何引用进行了评论,以尝试使用普通的旧文本框进行调试。起初我认为这是因为使用DateTimePicker有关,但它仍然会在注释掉后抛出错误。 )

C# code:

Cat temp = new Cat(txtCatName.Text, txtHair.Text, txtSize.Text, txtCareType.Text, txtNotes.Text);

public string AddCat()
    {
        string strFeedback = "";

        string strSQL = "INSERT INTO Cats (CatName, Hair, Size, CareType, Notes) VALUES (@CatName, @Hair, @Size, @CareType, @Notes)"; 

        OleDbConnection conn = new OleDbConnection();

        string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Data\Cats.accdb; Persist Security Info=False;";
        conn.ConnectionString = strConn; 

        OleDbCommand comm = new OleDbCommand();
        comm.CommandText = strSQL;  
        comm.Connection = conn; 
        comm.Parameters.AddWithValue("@CatName", CatName); 
        comm.Parameters.AddWithValue("@Hair", Hair);
        comm.Parameters.AddWithValue("@Size", Size);
        comm.Parameters.AddWithValue("@CareType", CareType);
        comm.Parameters.AddWithValue("@Notes", Notes);
        //comm.Parameters.AddWithValue("@AdoptDate", AdoptDate);
        //comm.Parameters.AddWithValue("@Weight", Weight);
        //comm.Parameters.AddWithValue("@Age", Age);

        {
            conn.Open();
            strFeedback = comm.ExecuteNonQuery().ToString() + " record has been added successfully!";
            conn.Close();
        }
        catch (Exception err)
        {
            strFeedback = "ERROR: " + err.Message;
        }
        return strFeedback;

lblFeedback.Text = temp.AddCat();

Thanks for any help you can give!

谢谢你提供的所有帮助!

2 个解决方案

#1


Size is a reserved keyword. Add brackets around the name to specify that it's an identifier:

Size是保留关键字。在名称周围添加括号以指定它是标识符:

string strSQL = "INSERT INTO Cats (CatName, Hair, [Size], CareType, Notes) VALUES (@CatName, @Hair, @Size, @CareType, @Notes)"; 

Alternatively, change the field name to something that is not a keyword.

或者,将字段名称更改为非关键字。

#2


In MS Access OLEDB I believe you use position markers rather than parameter names.

在MS Access OLEDB中,我相信您使用位置标记而不是参数名称。

string strSQL = "INSERT INTO Cats (CatName, Hair, Size, CareType, Notes) VALUES (?, ?, ?, ?, ?)";

#1


Size is a reserved keyword. Add brackets around the name to specify that it's an identifier:

Size是保留关键字。在名称周围添加括号以指定它是标识符:

string strSQL = "INSERT INTO Cats (CatName, Hair, [Size], CareType, Notes) VALUES (@CatName, @Hair, @Size, @CareType, @Notes)"; 

Alternatively, change the field name to something that is not a keyword.

或者,将字段名称更改为非关键字。

#2


In MS Access OLEDB I believe you use position markers rather than parameter names.

在MS Access OLEDB中,我相信您使用位置标记而不是参数名称。

string strSQL = "INSERT INTO Cats (CatName, Hair, Size, CareType, Notes) VALUES (?, ?, ?, ?, ?)";