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();
            }
  • 相关阅读:
    Go反射原理
    并发控制--context篇
    Go并发控制--WaitGroup篇
    Go依赖管理--module
    正睿培训 8.4 学习笔记
    bitset
    7.18 学习笔记
    7.17 学习笔记
    P6835 [Cnoi2020]线形生物
    UVA11300 Spreading the Wealth 思维题
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/13769506.html
Copyright © 2011-2022 走看看