0

我正在使用连接到 MS Access 的 C# Windows 应用程序表单。我正在用特定查询填充 datagridview 但这发生了

查询表达式 'Model WHERE Status = 'AVAILABLE'' 中的语法错误(缺少运算符)。

关于这个查询

OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available ORDER BY Type, Brand, Model WHERE Status = 'AVAILABLE'", cnn);

我该怎么办?

这是我的代码

OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\james\Documents\Visual Studio 2010\Projects\Vault\Vault\VaultDatabase.accdb");
DataSet dstAvailable = new DataSet();
DataSet dstData = new DataSet();
DataSet dstDeployment = new DataSet();
DataSet dstLog = new DataSet();


public void FillAvailable(DataGridView dgv)
{
    OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available ORDER BY Type, Brand, Model WHERE Status = 'AVAILABLE'", cnn);
    daAvailable.Fill(dstAvailable);
    dgv.DataSource = dstAvailable.Tables[0];
}
4

3 回答 3

1

ORDER BY子句应该WHERE在 SQL 中的子句之后

所以你的查询应该是:

OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, "+
     "Status, Remarks, RAM, HDD, ODD, VideoCard, PS " + 
     "FROM Available " +
     "WHERE Status = 'AVAILABLE' "+
     "ORDER BY Type, Brand, Model", cnn);

     //The above query is broken down on multiple lines for clarity
于 2013-05-16T09:18:24.413 回答
1

像这样写

public void FillAvailable(DataGridView dgv)
{
    OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available  WHERE Status = 'AVAILABLE' ORDER BY Type, Brand, Model", cnn);
    daAvailable.Fill(dstAvailable);
    dgv.DataSource = dstAvailable.Tables[0];
}

* Order By Comes After Where *

于 2013-05-16T09:19:03.257 回答
1

订购方式

总是在 WHERE 子句之后,它应该是:

OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available WHERE Status = 'AVAILABLE'  ORDER BY Type, Brand, Model", cnn);
于 2013-05-16T09:19:17.210 回答