我正在使用连接到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];
}
答案 0 :(得分:1)
ORDER BY
子句应该在SQL
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);
//The above query is broken down on multiple lines for clarity
答案 1 :(得分: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];
}
* 按顺序排列*
答案 2 :(得分:1)
ORDER BY
总是在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);