[英]Can't insert record into MS Access through C# oledb
我的MS Access數據庫中有一個名為“流派”的表。 它包含以下列:
這是我使用的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語句中的語法錯誤”。
我就是找不到問題。 我處理數據庫中數據的其他功能運行良好。
索引是Access中的保留關鍵字 。 您需要將其括在[]中。 嘗試這個:
INSERT INTO Genres([Index], GenreText) VALUES(@index, @text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.