大家好,请教大家一下,我刚学NET,遇到疑问,知道的朋友请帮忙一下,谢谢搜索: 国家(下拉) > 省级(下拉) > 市县(下拉) 关键字(文本框) 类别(下拉) 搜索(按钮)因为存在以下几种情况:
1.当国家,省级,市县,为空时,关键不为空,搜索结果
2.当国家不为空,关键字不为空,省级,市县为空时,搜索结果
3.当国家,省级不为空,市县,关键字为空时,搜索结果
4.当国家,省级,市县,都不为空时,关键字为空时,搜索结果.
5.当国家,省级,市县,关键字,都不为空时,搜索结果大致搜索就是这些情况,该如何在C#里面实现,我目前写的是这样,总出错,出现表达式错误!
//搜索关键字
string sProvince = ddlProvince.SelectedValue.ToString(); //国家
string sCity = ddlCity.SelectedValue.ToString(); //省级
string sVilliage = ddlVilliage.SelectedValue.ToString(); //市县
string sType = ToType.SelectedValue.ToString(); //分类
string sKey = keyWord.Text.Trim(); //关键字 switch (sType)
{
case "请选择":
break;
case "No": //当选择搜索编号时 if (sProvince != null && sCity == null) //当国家不为空,城市,区县,关键字为空时,执行
{
s = s + " where 国家字段= " + sProvince;
} else if (sProvince != null && sCity != null) //当国家,省级不为空时
{
s = s + " where 国家字段= " + sProvince + " and 省级字段=" + sCity;
}
return;
break;
case "Wname":
s = s + " where 字段 like " + "'%" + sKey + "%' OR 字段 like " + "'%" + sKey + "%'";
break;
}谢谢大家
1.当国家,省级,市县,为空时,关键不为空,搜索结果
2.当国家不为空,关键字不为空,省级,市县为空时,搜索结果
3.当国家,省级不为空,市县,关键字为空时,搜索结果
4.当国家,省级,市县,都不为空时,关键字为空时,搜索结果.
5.当国家,省级,市县,关键字,都不为空时,搜索结果大致搜索就是这些情况,该如何在C#里面实现,我目前写的是这样,总出错,出现表达式错误!
//搜索关键字
string sProvince = ddlProvince.SelectedValue.ToString(); //国家
string sCity = ddlCity.SelectedValue.ToString(); //省级
string sVilliage = ddlVilliage.SelectedValue.ToString(); //市县
string sType = ToType.SelectedValue.ToString(); //分类
string sKey = keyWord.Text.Trim(); //关键字 switch (sType)
{
case "请选择":
break;
case "No": //当选择搜索编号时 if (sProvince != null && sCity == null) //当国家不为空,城市,区县,关键字为空时,执行
{
s = s + " where 国家字段= " + sProvince;
} else if (sProvince != null && sCity != null) //当国家,省级不为空时
{
s = s + " where 国家字段= " + sProvince + " and 省级字段=" + sCity;
}
return;
break;
case "Wname":
s = s + " where 字段 like " + "'%" + sKey + "%' OR 字段 like " + "'%" + sKey + "%'";
break;
}谢谢大家
strSql+=string.IsNullOrEmpty(sProvince)?"":" and 国家字段='"+sProvince+"'";
或使用StringBuilder
A:地址类(国家、省、市)
B:关键字要求A、B不能同时为空,否则不执行搜索?
把你拼的SQL语句 response.write 出来,看问题出在哪里.
搜索条件用一个结构体来容纳所有参数,在搜索方法里拼接sql语句
前台根据用户的选择来初始化搜索条件结构体这样就清楚和容易得多