C# MS Access中的SQL查询未检索C中的值
我创建了一个从MSAccess 2007数据库检索结果的方法,但它不返回任何值。但是,当我复制sql语句并将其粘贴到msaccess查询设计sql视图时,我得到了预期的结果。 我上传了结果的图片。 当我从tblItems中检索所有SELECT*值时,它非常有效。但当我添加一个条件时,我的C应用程序中不会显示结果。 这是我的密码 类别:DBConnectionC# MS Access中的SQL查询未检索C中的值,c#,ms-access,ms-access-2007,C#,Ms Access,Ms Access 2007,我创建了一个从MSAccess 2007数据库检索结果的方法,但它不返回任何值。但是,当我复制sql语句并将其粘贴到msaccess查询设计sql视图时,我得到了预期的结果。 我上传了结果的图片。 当我从tblItems中检索所有SELECT*值时,它非常有效。但当我添加一个条件时,我的C应用程序中不会显示结果。 这是我的密码 类别:DBConnection public List<Item> getItems(string sql) { List<Item>
public List<Item> getItems(string sql)
{
List<Item> allItems = new List<Item>();
Item thisItem = null;
openConnection();
command = new OleDbCommand(sql, con);
reader = command.ExecuteReader();
if (reader.Read())
{
do
{
thisItem = new Item();
thisItem.setItemID(Int32.Parse(reader["itemID"].ToString()));
thisItem.setBarcode(reader["barcode"].ToString());
thisItem.setBrandID(Int32.Parse(reader["brandID"].ToString()));
thisItem.setCategoryID(Int32.Parse(reader["categoryID"].ToString()));
thisItem.setCode(reader["code"].ToString());
thisItem.setDescription(reader["description"].ToString());
thisItem.setPrice(Double.Parse(reader["price"].ToString()));
allItems.Add(thisItem);
}
while (reader.Read());
}
else
{
thisItem = null;
}
closeConnection();
return allItems;
}
表格编号:
private void btnFilter_Click(object sender, EventArgs e)
{
//MyFunctions func = new MyFunctions();
DBConnection dbCon = new DBConnection();
string brandID = cboBrand.SelectedValue.ToString().Equals("0") ? " > 0" : " = " + cboBrand.SelectedValue.ToString();
string categoryID = cboCategory.SelectedValue.ToString().Equals("0") ? " > 0" : " = " + cboCategory.SelectedValue.ToString();
string description = txtDescription.Text;
string code = txtCode.Text;
string barcode = txtBarcode.Text;
loadProducts(dbCon.getItems("SELECT * FROM tblItems WHERE [brandID] " + brandID + " AND [categoryID] " + categoryID + " AND [description] LIKE '*" + description + "' AND [code] LIKE '*" + code + "' AND [barcode] LIKE '*" + barcode + "'"));
}
public void loadProducts(List<Item> allItems)
{
MyFunctions func = new MyFunctions();
int i = 0;
lvwItems.Items.Clear();
foreach (Item thisItem in allItems)
{
lvwItems.Items.Add(thisItem.getItemID().ToString());
lvwItems.Items[i].SubItems.Add(thisItem.getBarcode());
lvwItems.Items[i].SubItems.Add(thisItem.getCategoryName());
lvwItems.Items[i].SubItems.Add(thisItem.getBrandName());
lvwItems.Items[i].SubItems.Add(thisItem.getCode());
lvwItems.Items[i].SubItems.Add(thisItem.getDescription());
lvwItems.Items[i].SubItems.Add(func.toCurrency(thisItem.getPrice()));
i++;
}
}
至于您的屏幕截图,brandID和categoryID都不是字符串,但是您的SQL是不正确的。应该是这样写的:
loadProducts(dbCon.getItems("SELECT * FROM tblItems WHERE [brandID] = " + brandID + " AND [categoryID] = " + categoryID + " AND [description] LIKE '*" + description + "' AND [code] LIKE '*" + code + "' AND [barcode] LIKE '*" + barcode + "'"));
解决了我刚把通配符*换成%。。谢谢特别是对于@Wery Nguyen。您的brandID、CategoryId是字符串,应该用单引号括起来。我认为通配符在C表达式中是不同的。在类似的情况下尝试使用%而不是*。Sachu和Gustav:。我删除brandID和categoryID语句中的=符号,因为我有一个条件。请参阅btnFilter_Click事件中的分配语句。它将检索brandID和category,包括=号或>号。我在brandID和categoryID语句中删除了=号,因为我有一个条件。请参阅btnFilter_Click事件中的分配语句。它将检索brandID和categoryID的值,包括=号或>号。@user4938771在执行此查询之前,请确认,然后将其分配到字符串变量并执行消息框。然后检查查询的值是否正确。@user4938771我想问题是分配了=和>符号。。在大于符号中,您没有连接选定的类别id和批次id。请参阅我编辑的答案。解决了它。。我通过即时窗口跟踪了SQL语句,它是正确的。。问题是,msaccess中使用的通配符不同于C。I将*替换为%。谢谢
loadProducts(dbCon.getItems("SELECT * FROM tblItems WHERE [brandID] = " + brandID + " AND [categoryID] = " + categoryID + " AND [description] LIKE '*" + description + "' AND [code] LIKE '*" + code + "' AND [barcode] LIKE '*" + barcode + "'"));