c#中如何实现多条件查询?
答案:4 悬赏:20 手机版
解决时间 2021-02-11 23:31
- 提问者网友:十年饮冰
- 2021-02-11 02:34
c#中如何实现多条件查询?
最佳答案
- 五星知识达人网友:忘川信使
- 2021-02-11 03:02
#region 多条件搜索时,使用List集合来拼接条件(拼接Sql)
StringBuilder sql = new StringBuilder("select * from PhoneNum");
List wheres = new List();
if (cboGroup.SelectedIndex != 0)
{
wheres.Add(" ptypeid=" + cboGroup.Text.Split('|')[0]);
}
if (txtSearchName.Text.Trim().Length > 0)
{
wheres.Add(" pname like '%" + txtSearchName.Text.Trim() + "%'");
}
if (txtSearchCellPhone.Text.Trim().Length > 0)
{
wheres.Add(" pcellphone like '%" + txtSearchCellPhone.Text.Trim() + "%'");
}
//判断用户是否选择了条件
if (wheres.Count > 0)
{
string wh = string.Join(" and ", wheres.ToArray());
sql.Append(" where " + wh);
}
#endregion
#region 多条件搜索使用带参数的sql语句
StringBuilder sql = new StringBuilder("select * from PhoneNum");
List wheres = new List();
List listParameter = new List();
if (cboGroup.SelectedIndex != 0)
{
wheres.Add(" ptypeid=@typeid ");
listParameter.Add(new SqlParameter("@typeid", cboGroup.Text.Split('|')[0]));
}
if (txtSearchName.Text.Trim().Length > 0)
{
wheres.Add(" pname like @pname ");
//pname like '%乔%'
//pname liek '%'+@pname+'%'
listParameter.Add(new SqlParameter("@pname", "%" + txtSearchName.Text.Trim() + "%"));
}
if (txtSearchCellPhone.Text.Trim().Length > 0)
{
wheres.Add(" pcellphone like @cellphone ");
listParameter.Add(new SqlParameter("@cellphone", "%" + txtSearchCellPhone.Text.Trim() + "%"));
}
//判断用户是否选择了条件
if (wheres.Count > 0)
{
string wh = string.Join(" and ", wheres.ToArray());
sql.Append(" where " + wh);
}
SqlHelper.ExecuteDataTable(sql.ToString(), listParameter.ToArray());
#endregion
StringBuilder sql = new StringBuilder("select * from PhoneNum");
List
if (cboGroup.SelectedIndex != 0)
{
wheres.Add(" ptypeid=" + cboGroup.Text.Split('|')[0]);
}
if (txtSearchName.Text.Trim().Length > 0)
{
wheres.Add(" pname like '%" + txtSearchName.Text.Trim() + "%'");
}
if (txtSearchCellPhone.Text.Trim().Length > 0)
{
wheres.Add(" pcellphone like '%" + txtSearchCellPhone.Text.Trim() + "%'");
}
//判断用户是否选择了条件
if (wheres.Count > 0)
{
string wh = string.Join(" and ", wheres.ToArray());
sql.Append(" where " + wh);
}
#endregion
#region 多条件搜索使用带参数的sql语句
StringBuilder sql = new StringBuilder("select * from PhoneNum");
List
List
if (cboGroup.SelectedIndex != 0)
{
wheres.Add(" ptypeid=@typeid ");
listParameter.Add(new SqlParameter("@typeid", cboGroup.Text.Split('|')[0]));
}
if (txtSearchName.Text.Trim().Length > 0)
{
wheres.Add(" pname like @pname ");
//pname like '%乔%'
//pname liek '%'+@pname+'%'
listParameter.Add(new SqlParameter("@pname", "%" + txtSearchName.Text.Trim() + "%"));
}
if (txtSearchCellPhone.Text.Trim().Length > 0)
{
wheres.Add(" pcellphone like @cellphone ");
listParameter.Add(new SqlParameter("@cellphone", "%" + txtSearchCellPhone.Text.Trim() + "%"));
}
//判断用户是否选择了条件
if (wheres.Count > 0)
{
string wh = string.Join(" and ", wheres.ToArray());
sql.Append(" where " + wh);
}
SqlHelper.ExecuteDataTable(sql.ToString(), listParameter.ToArray());
#endregion
全部回答
- 1楼网友:长青诗
- 2021-02-11 05:05
前提是你的多条件查询是在什么里面的查询;如果是datagridview中那就是很简单的多条件的查询;如果是在其他的里面就是另外的一种查询的方法用到的是SQL的查询;
- 2楼网友:执傲
- 2021-02-11 03:34
string sqlstr = string.format(@"select * from houses where hmoney between '{0}' and '{1}' and harea between '{2}' and '{3}'", min_money, max_money, min_area, max_area);
现改成这种写法,格式一目了然。另外,有一个问题,min_money, max_money, min_area, max_area都用单引号括起来了,这样会转变成字符串,在between..and里面用字符串???是你的本意吗?如果是数值之间的判断,要把单引号去掉!!
- 3楼网友:神鬼未生
- 2021-02-11 03:22
public DataSet GetSelectBaseIndex(string Branch, string Name)
{
string Condition = " where 1=1 ";
if (Branch != "")
{
Condition += " and Branch = '" + Branch.Replace("'", "''") + "' ";
}
if (Name!= "")
{
Condition += " and Name= '" + Name.Replace("'", "''") + "' ";
}
string SelectString = "select * from Table " + Condition + " order by Name";
return SqlHelper.ExecuteDataset(this.connectionString, CommandType.Text, SelectString);
}
参考
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯