我在創建多個TextBox
和ComboBox
搜索時遇到問題。Windows窗體中的多個TextBox和ComboBox搜索使用MS-Access的應用程序C#
我用下面的查詢第一:
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查詢嚮導,它是什麼做的是正在運行的時候,我不任何參數傳遞給它即所有參數的空它顯示所有內容。而當我傳遞單一參數的時候,它並沒有給我任何東西,只是顯示空白表。 而現在我使用這個查詢使用CommandText
和傳遞參數使用Parameters.Add
我得到error
一個或多個參數丟失。
任何人都可以向我提供的解決方案是什麼我已經使用在SQL Server這給正確的結果相同類型的查詢在MS-Access數據庫
做搜索的方式。
我使用下面的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
和當我使用IFF
OR
條件,當用戶什麼都沒有提供到TextBox
或ComboBox
它的DataGrid
沒有顯示任何內容。它只在用戶提供至少一些值時才起作用TextBox
或ComboBox
得到你的觀點先生。但仍然如何可以忽略where子句中的特定參數比較,當用戶不'鍵入/選擇任何東西? – analyticalpicasso
我在我的答案中添加了一種方式(不是最優雅的)。 – dovid
當我動態添加參數時,應該如何使我的主要選擇查詢應該結束,直到需要排除的地方或者我需要排除的地方? – analyticalpicasso