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

Posted

技术标签:

【中文标题】无法通过 C# oledb 将记录插入 MS Access【英文标题】:Can't insert record into MS Access through C# oledb 【发布时间】:2015-05-01 21:01:41 【问题描述】:

我的 MS Access 数据库中有一个名为“流派”的表。它有以下列:

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 语句中的语法错误。”

我只是找不到问题。我用于处理数据库数据的其他功能工作正常。

【问题讨论】:

索引是 MS Access 中的保留字。将单词括在括号中,如下所示:Genres([Index], GenreText) @Fink,这就是问题所在。非常感谢。在过去的几个小时里,我一直在为此苦苦挣扎。 【参考方案1】:

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

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

【讨论】:

以上是关于无法通过 C# oledb 将记录插入 MS Access的主要内容,如果未能解决你的问题,请参考以下文章