C# 使用MS Access在Windows窗体应用程序C中搜索多个文本框和组合框

C# 使用MS Access在Windows窗体应用程序C中搜索多个文本框和组合框,c#,sql,ms-access,C#,Sql,Ms Access,我在创建多个TextBox和ComboBoxsearch时遇到问题 我首先使用了以下查询: 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_

我在创建多个TextBox和ComboBoxsearch时遇到问题

我首先使用了以下查询:

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传递参数时,我发现一个或多个参数丢失了

有谁能给我提供一个解决方案,在MS Access数据库中进行搜索的方法是什么

我在SQLServer中使用了相同类型的查询,可以提供正确的结果

我正在使用以下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时,或者当用户未向文本框或组合框提供任何内容时,其在数据网格中未显示任何内容。只有当用户向每个文本框或组合框至少提供一些值时,它才起作用。您不能在查询语句中引用同一参数两次。因为每次它引用一个新参数

OLE DB.NET提供程序不支持用于传递的命名参数 由调用的SQL语句或存储过程的参数 CommandType设置为Text时的OleDbCommand。在这种情况下 必须使用问号占位符。例如:

从CustomerID=?的客户中选择*

因此 必须将OLEDBPParameter对象添加到OLEDBPParameterCollection 直接对应于问号占位符的位置 用于命令文本中的参数

相反,您必须根据您的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 + "%");
}

明白了,先生。但是,当用户没有键入/选择任何内容时,我怎么能忽略where子句中的特定参数比较呢?我在回答中添加了一种不是最优雅的方式。当我动态添加参数时,我应该如何进行主选择查询我应该将其结束到何处还是我还需要排除在何处?您的概念很优雅,先生:。