簡體   English   中英

使用MS-Access在Windows窗體應用程序C#中進行多個TextBox和ComboBox搜索

[英]Multiple TextBox and ComboBox Search in Windows Form Application C# using MS-Access

我在創建多個TextBoxComboBox搜索時遇到麻煩。

我首先使用以下查詢:

SELECT p.property_id, p.property_name, s.type_name, p.property_purpose, p.property_price, p.area, p.bedrooms, p.property_location, c.customer_name, c.customer_mobile1
FROM (tb_property AS p INNER JOIN lk_tb_property_type AS s ON p.property_type_id=s.property_type_id) INNER JOIN tb_customer AS c ON p.customer_id=c.customer_id
WHERE ([@propertyType] Is Null Or s.type_name Like '%'+[@propertyType]+'%') And ([@propertyPurpose] Is Null Or p.property_purpose Like '%'+[@propertyPurpose]+'%') And ([@area] Is Null Or p.area Like '%'+[@area]+'%') And ([@bedrooms] Is Null Or p.bedrooms Like '%'+[@bedrooms]+'%') And ([@price] Is Null Or p.property_price Like '%'+[@price]+'%') And ([@buidName] Is Null Or p.property_name Like '%'+p.property_name+'%') And ([@location] Is Null Or p.property_location Like '%'+[@location]+'%');

當我在MS-Access的查詢向導中運行時,當我不向其傳遞任何參數(即所有參數為null)時,它將顯示所有內容。 當我傳遞單個參數時,它什么也沒給我,僅顯示空白表。 而目前我使用這個查詢中使用CommandText ,並通過使用參數Parameters.Add我得到error一個或多個參數丟失

誰能為我提供MS-Access DB中進行搜索的方式是什么的解決方案

我在SQL Server中使用了相同類型的查詢,它可以提供正確的結果。

我正在使用以下c#代碼:

string propertyType = combo_prop_type.Text;
if (propertyType == "Select Property Type")
{
    propertyType = null;
}
else
{
    propertyType = combo_prop_type.Text;
}

string propertyPurpose = combo_purpose.Text;
if (propertyPurpose == "Select Property Purpose")
{
    propertyPurpose = null;
}
else
{
    propertyType = combo_prop_type.Text;
}

string area = combo_area.Text;
if (area == "Select Area")
{
    area = null;
}
else
{
    area = combo_area.Text.ToString();
}

string bedrooms = combo_bedrooms.Text;
if (bedrooms == "Select Bedrooms")
{
    bedrooms = null;
}
else
{
    bedrooms = combo_bedrooms.Text.ToString();
}

string price = combo_price.Text;
if (price == "Select Price")
{
    price = null;
}
else
{
    price = combo_price.Text.ToString();
}

string buidName = txt_build_name.Text.Trim();
string location = txt_location.Text.Trim();

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;
cmd.CommandText = "select p.property_id, p.property_name, s.type_name, p.property_purpose, p.property_price, p.area, p.property_location, c.customer_name, c.customer_mobile1  from ((tb_property p INNER JOIN lk_tb_property_type s ON p.property_type_id = s.property_type_id) INNER JOIN tb_customer c ON p.customer_id = c.customer_id) WHERE (@propertyType is null or s.type_name like '%' + @propertyType + '%') and (@propertyPurpose is null or p.property_purpose like '%'+ @propertyPurpose +'%') and (@area is null or p.area like '%'+ @area +'%') and (@bedrooms is null or p.bedrooms like '%'+ @bedrooms +'%') and (@price is null or p.property_price like '%'+ @price +'%') and (@buidName is null or p.property_name like '%'+ @buildName +'%') and (@location is null or p.property_location like '%'+ @location +'%')";
cmd.Parameters.Add("@propertyType", OleDbType.VarChar, 50).Value = propertyType;
cmd.Parameters.Add("@propertyPurpose", OleDbType.VarChar, 50).Value = propertyPurpose;
cmd.Parameters.Add("@area", OleDbType.VarChar, 50).Value = area;
cmd.Parameters.Add("@bedrooms", OleDbType.VarChar, 50).Value = bedrooms;
cmd.Parameters.Add("@price", OleDbType.VarChar, 50).Value = price;
cmd.Parameters.Add("@buildName", OleDbType.VarChar, 50).Value = buidName;
cmd.Parameters.Add("@location", OleDbType.VarChar, 50).Value = location;
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{

    for (int j = 0; j < 9; j++)
    {
        dataGridView2.Rows.Add(new DataGridViewRow());
        dataGridView2.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
    }
}
con.Close();

我還搜索了IFF()COALESCE()NZ()是以下函數,當我們需要發送默認值時使用,但對於COALESCE()我在MS-ACCESS獲得未定義的函數,而當我在使用IFFOR條件是,當用戶不向TextBoxComboBox提供任何內容時,它不會向DataGrid顯示任何內容。 當用戶為每個TextBoxComboBox提供至少一些值時,它才起作用

您不能在查詢語句中兩次引用相同的參數。 因為每次它都引用一個新參數!

MSDN-OleDbCommand.Parameters

當CommandType設置為Text時,OLE DB .NET Provider不支持將命名參數傳遞給SQL語句或OleDbCommand調用的存儲過程的命名參數。 在這種情況下,必須使用問號(?)占位符。 例如:

在客戶ID =?的情況下從客戶中選擇*

因此,將OleDbParameter對象添加到OleDbParameterCollection的順序必須直接對應於命令文本中參數的問號占位符的位置。

相反,您將必須根據UI模式來構建查詢和動態參數。

像這樣:

string propertyType = combo_prop_type.Text;
if (propertyType == "Select Property Type")
{
    propertyType = null;
}
else
{
    da.SelectCommand.CommandText += " AND s.type_name LIKE ? ";
    da.SelectCommand.Parameters.Add("", "%" + combo_prop_type.Text + "%");
}

暫無
暫無

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

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