我有一个包含5列的数据表,(歌曲,艺术家,专辑,流派,时间)表格允许我输入尽可能多的行,因为我想创建音乐播放列表,当用户认为合适时,他们可以点击按钮导出要访问的数据。我的访问数据库有一个名为“Playlist”的表,其中包含与数据表相同的5列。当尝试传输数据时,我不断收到Insert INTO语句的异常错误,我不知道为什么因为我使用的是commandBuilder。我附上了执行此操作的类和方法。
请指教!
public void ExportPlaylistToAccess(DataTable playlist)
{
// open connection to the database pathed to
String connection = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data source= D:\CIS 465\Final Project\VirtualiPod\iTunesPlaylistDatabase.accdb";
using (OleDbConnection con = new OleDbConnection(connection))
{
var adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand("SELECT * from [Playlist]", con);
var cbr = new OleDbCommandBuilder(adapter);
cbr.GetDeleteCommand();
cbr.GetInsertCommand();
cbr.GetUpdateCommand();
try
{
con.Open();
adapter.Update(playlist);
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message, "Database Error");
}
catch (Exception x)
{
MessageBox.Show(x.Message, "Exception Error");
}
}
dataTable creation
private void createPlaylist_Click(object sender, EventArgs e)
{
if (playlist.Rows.Count == 0)
{
playlist.Columns.Add("Song");
playlist.Columns.Add("Artist");
playlist.Columns.Add("Album");
playlist.Columns.Add("Genre");
playlist.Columns.Add("Time");
dataGridView1.DataSource = playlist;
}
else if (playlist.Rows.Count > 0)
{
MessageBox.Show("Please clear your current playlist to create a new one.");
}
}
// adds song to playlist for user upon click
private void addToPlaylist_Click(object sender, EventArgs e)
{
IITTrackCollection tracks = app.LibraryPlaylist.Tracks;
IITTrack currentTrack = app.CurrentTrack;
DataRow newRow;
newRow = playlist.NewRow();
newRow["Song"] = currentTrack.Name;
newRow["Artist"] = currentTrack.Artist;
newRow["Album"] = currentTrack.Album;
newRow["Genre"] = currentTrack.Genre;
newRow["Time"] = currentTrack.Time;
playlist.Rows.Add(newRow);
dataGridView1.DataSource = playlist;
}
答案 0 :(得分:1)
时间是一个保留词。由于某种原因,命令构建器不包含数据库保留字(时间,日期,长度等)的字段,括号[time]允许插入查询正常工作。如果没有括号,插入将失败,因为SQL编译器不知道字符串时间是sql命令还是字段名称。我发现的唯一解决方案是重命名数据库字段,以便它们不与数据库保留名称冲突。希望MS最终能解决这个错误。