无法通过 C# oledb 将记录插入 MS Access

Can't insert record into MS Access through C# oledb

我的 MS Access 数据库中有一个名为 "Genres" 的 table。它有以下列:

这是我使用的 C# 代码:

public static void AddGenre(string text, int index)
    {
        string query = "INSERT INTO Genres(Index, GenreText) VALUES(@index, @text)";
        using (OleDbConnection con = new OleDbConnection(connectionString))
        {
            try
            {
                OleDbCommand command = new OleDbCommand(query, con);

                command.Parameters.AddWithValue("@index", index);
                command.Parameters.AddWithValue("@text", text);

                con.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new ArgumentException();
            }
        }
    }

问题是我总是遇到异常"Syntax error in INSERT INTO statement."

我就是找不到问题所在。我用于操作数据库中数据的其他功能工作正常。

索引是Access中的reserved keyword。您需要将其括在 [] 中。试试这个:

INSERT INTO Genres([Index], GenreText) VALUES(@index, @text)