在C#Access中使用INNER JOIN时出错

时间:2016-07-15 06:01:08

标签: c# access

为什么在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);
}

enter image description here

1 个答案:

答案 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的关键字列表。它可能已经增长:

Access Reserved words