c#中如何实现多条件查询?
4个回答
展开全部
#region 多条件搜索时,使用List集合来拼接条件(拼接Sql)
StringBuilder sql = new StringBuilder("select * from PhoneNum");
List<string> wheres = new List<string>();
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<string> wheres = new List<string>();
List<SqlParameter> listParameter = new List<SqlParameter>();
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<string> wheres = new List<string>();
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<string> wheres = new List<string>();
List<SqlParameter> listParameter = new List<SqlParameter>();
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
展开全部
前提是你的多条件查询是在什么里面的查询;如果是datagridview中那就是很简单的多条件的查询;如果是在其他的里面就是另外的一种查询的方法用到的是SQL的查询;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
参考
{
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);
}
参考
追问
您能详细点吗?我是菜鸟儿。不怎么明白。刚刚接触这个。求解释!
追答
那你现在能用的代码复制出来。。。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询