簡體   English   中英

MS Access中的SQL查詢未檢索C#中的值

[英]SQL query in MS Access not retrieving values in C#

我創建了一個從MSAccess 2007數據庫中檢索結果的方法,但它不返回任何值。 但是,當我復制sql語句並將其粘貼到msaccess查詢設計(SQL VIEW)上時,我得到了預期的結果。

我上傳了其結果圖片。 在此處輸入圖片說明

當我檢索所有值“ SELECT * FROM tblItems”時,它完美地工作。 但是當我添加一個條件時,我的C#應用​​中沒有顯示任何結果。 這是我的代碼。

類: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++;
        }
    }
"SELECT * FROM tblItems WHERE [brandID] " + brandID 
        + " AND [categoryID] " + categoryID +
         " AND [description] LIKE '*" + description 
       + "' AND [code] LIKE '*" + code 
       + "' AND [barcode] LIKE '*" + barcode + "'"

解決了..:DI剛將通配符“ *”替換為“%” ..尤其是@Wery Nguyen。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM