MS Access中的SQL查询无法在C#中检索值

时间:2015-05-26 04:11:29

标签: c# ms-access ms-access-2007

我创建了一个从MSAccess 2007数据库中检索结果的方法,但它没有返回任何值。但是当我复制sql语句并将其粘贴到msaccess查询设计(SQL VIEW)上时,我得到了我期望的结果。

我上传了结果图片。 enter image description here

当我检索所有值“SELECT * FROM tblItems”时,它完全有效。但是当我添加条件时,我的C#应用​​程序中没有显示结果。 这是我的代码。

CLASS:DBConnection

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++;
        }
    }

3 个答案:

答案 0 :(得分:0)

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
header("Content-Type: text/html; charset=UTF-8");

答案 1 :(得分:0)

解决了..:D我刚刚更换了通配符&#34; *&#34; to&#34;%&#34; ..谢谢..特别是@Wery Nguyen。

答案 2 :(得分:-1)

至于您的屏幕截图,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 + "'"));