为什么在C#中使用Inner join时出现错误?当我在Access中尝试时,查询正在运行。
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "SELECT BookID, BookName, CategoryDescription, Language, AuthorName, YearPublished FROM tblCategory INNER JOIN (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) ON tblCategory.CategoryID = tblBook.CategoryID";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
答案 0 :(得分:2)
实际上,这是因为您在查询中使用了关键字language
。您可以通过将其更改为[Language]
从
更改您的查询string query = "SELECT BookID, BookName, CategoryDescription, Language, AuthorName, YearPublished FROM tblCategory INNER JOIN (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) ON tblCategory.CategoryID = tblBook.CategoryID";
到
string query = "SELECT BookID, BookName, CategoryDescription, [Language], AuthorName, YearPublished FROM tblCategory INNER JOIN (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) ON tblCategory.CategoryID = tblBook.CategoryID";
为了安全起见,请始终在所有列名和表名周围使用方括号([])。
为了将来参考,这里是Access 2007的关键字列表。它可能已经增长: