2023年6月21日发(作者:)

C#中多条件拼接SQL语句#region 多条件搜索时,使⽤List集合来拼接条件(拼接Sql)StringBuilder sql = new StringBuilder("select * from PhoneNum");List wheres = new List();if (edIndex != 0){(" ptypeid=" + ('|')[0]);}if (().Length > 0){(" pname like '%" + () + "%'");}if (().Length > 0){(" pcellphone like '%" + () + "%'");}//判断⽤户是否选择了条件if ( > 0){string wh = (" and ", y());(" where " + wh);}#endregion#region 多条件搜索使⽤带参数的sql语句StringBuilder sql = new StringBuilder("select * from PhoneNum");List wheres = new List();List listParameter = new List();if (edIndex != 0){(" ptypeid=@typeid ");(new SqlParameter("@typeid", ('|')[0]));}if (().Length > 0){(" pname like @pname ");//pname like '%乔%'//pname liek '%'+@pname+'%'(new SqlParameter("@pname", "%" + () + "%"));}if (().Length > 0){(" pcellphone like @cellphone ");(new SqlParameter("@cellphone", "%" + () + "%"));}//判断⽤户是否选择了条件if ( > 0){string wh = (" and ", y());(" where " + wh);}eDataTable(ng(), y());#endregion注意:1.如果参数化,底层⽤dapper,则直接将参数写⼊,然后不⽤管if,最后把所有可能的参数都写成匿名对象,直接传过去,就可以了。2.如果是 sqlparameters 这样的话,参照上边 第2个例⼦

2023年6月21日发(作者:)

C#中多条件拼接SQL语句#region 多条件搜索时,使⽤List集合来拼接条件(拼接Sql)StringBuilder sql = new StringBuilder("select * from PhoneNum");List wheres = new List();if (edIndex != 0){(" ptypeid=" + ('|')[0]);}if (().Length > 0){(" pname like '%" + () + "%'");}if (().Length > 0){(" pcellphone like '%" + () + "%'");}//判断⽤户是否选择了条件if ( > 0){string wh = (" and ", y());(" where " + wh);}#endregion#region 多条件搜索使⽤带参数的sql语句StringBuilder sql = new StringBuilder("select * from PhoneNum");List wheres = new List();List listParameter = new List();if (edIndex != 0){(" ptypeid=@typeid ");(new SqlParameter("@typeid", ('|')[0]));}if (().Length > 0){(" pname like @pname ");//pname like '%乔%'//pname liek '%'+@pname+'%'(new SqlParameter("@pname", "%" + () + "%"));}if (().Length > 0){(" pcellphone like @cellphone ");(new SqlParameter("@cellphone", "%" + () + "%"));}//判断⽤户是否选择了条件if ( > 0){string wh = (" and ", y());(" where " + wh);}eDataTable(ng(), y());#endregion注意:1.如果参数化,底层⽤dapper,则直接将参数写⼊,然后不⽤管if,最后把所有可能的参数都写成匿名对象,直接传过去,就可以了。2.如果是 sqlparameters 这样的话,参照上边 第2个例⼦