zoukankan      html  css  js  c++  java
  • 使用Dapper参数化查询(一)

    封装查询实体类

    public class QueryParameter
        {
            public QueryParameter()
            {
                listWhere = new List<string>();
                dynamicParameter = new DynamicParameters();
            }
    
            public List<string> listWhere { get; set; }
            public DynamicParameters dynamicParameter { get; set; }
    
            public string strWhere
            {
                get
                {
                    if (listWhere != null && listWhere.Count > 0)
                        return string.Join(" AND ", listWhere);
                    else
                        return "";
                }
            }
        }

    使用时候

    qp = new QueryParameter();
                if (!string.IsNullOrWhiteSpace(txtcname.Text))
                {
                    _qp.listWhere.Add(" cname LIKE @cname ");
                    _qp.dynamicParameter.Add("@cname", "%" + txtcname.Text.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(txtcaccount.Text))
                {
                    _qp.listWhere.Add(" caccount LIKE @caccount ");
                    _qp.dynamicParameter.Add("@caccount", "%" + txtcaccount.Text.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(txtcinput.Text))
                {
                    _qp.listWhere.Add(" cinput=@cinput ");
                    _qp.dynamicParameter.Add("@cinput", txtcinput.Text.Trim());
                }
    /// <summary>
            /// 获得数据列表
            /// </summary>
            public List<op_company> GetList(QueryParameter qp)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT * FROM op_company ");
                if (!string.IsNullOrEmpty(qp.strWhere))
                {
                    strSql.Append(" WHERE " + qp.strWhere);
                }
                return DBHelperFactory.RemoteWrite.DbConnection.Query<op_company>(strSql.ToString(), qp.dynamicParameter).ToList();
            }
  • 相关阅读:
    mongoTemplate.aggregate()聚合查询
    解决ElasticSearch5.x中@Field注解之IK分词不能用的问题
    Mybatis中使用Enum传参
    过滤,去重filter,去重reduce
    自己写的数组 方法的组合使用
    uni-app 使用vuex的方法
    uni-app实战写法
    vue的bug问题
    vuex
    vue webapp的基本功能实现方法
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/13769506.html
Copyright © 2011-2022 走看看