我的 MS Access 数据库中有一个名为“Genres”的表。它具有以下列:
- ID -> 自动编号
- 索引 -> 数字
- 流派文本 -> 文本
这是我使用的 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();
}
}
}
问题是我总是遇到异常“INSERT INTO 语句中的语法错误。”
我就是找不到问题所在。我用于操作数据库数据的其他功能运行良好。
最佳答案
索引是 reserved keyword 在 Access 中。您需要将其括在 [] 中。试试这个:
INSERT INTO Genres([Index], GenreText) VALUES(@index, @text)
关于c# - 无法通过 C# oledb 将记录插入 MS Access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995094/