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

SQL带参数拼接,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。