SQL带参数拼接
List<SqlParameter> paras = new List<SqlParameter>(); string wherSql = PreWhereSQL + GetQuerySql(paras); string GetQuerySql(List<SqlParameter> paras) { StringBuilder where = new StringBuilder(" and 1=1"); if (!string.IsNullOrEmpty(tbWorkOrderNo.Text.Trim())) { where.Append(" and SgWorkOrderNo like @p_SgWorkOrderNo"); paras.Add(new SqlParameter("@p_SgWorkOrderNo", "%" + tbWorkOrderNo.Text.Trim() + "%")); } if (!string.IsNullOrEmpty(tbTitle.Text.Trim())) { where.Append(" and Title like @p_tbTitle"); paras.Add(new SqlParameter("@p_tbTitle", "%" + tbTitle.Text.Trim() + "%")); } if (!string.IsNullOrEmpty(ddlCity.SelectedValue)) { where.Append(" and CityName = @p_CityName"); paras.Add(new SqlParameter("@p_CityName", ddlCity.SelectedValue)); } if (!string.IsNullOrEmpty(ddlBussinessAttr.SelectedValue)) { where.Append(" and ProductID = @p_ProductID"); paras.Add(new SqlParameter("@p_ProductID", ddlBussinessAttr.SelectedValue)); } if (!string.IsNullOrEmpty(tbStartTimeS.Text.Trim())) { where.Append(" and JieSuanStartTime >= @p_JieSuanStartTimeS"); paras.Add(new SqlParameter("@p_JieSuanStartTimeS", tbStartTimeS.Text.Trim())); } if (!string.IsNullOrEmpty(tbStartTimeE.Text.Trim())) { where.Append(" and JieSuanStartTime <= @p_JieSuanStartTimeE"); paras.Add(new SqlParameter("@p_JieSuanStartTimeE", tbStartTimeE.Text.Trim())); } if (!string.IsNullOrEmpty(ddlCheckStatus.SelectedValue)) { where.Append(" and JieSuanStatus = @p_JieSuanStatus"); paras.Add(new SqlParameter("@p_JieSuanStatus", ddlCheckStatus.SelectedValue)); } return where.ToString(); } 这个是有前置查询条件的,所有where的初始值为 and 1=1