dapper利用DynamicParameters构建动态参数查询

public static int GetTotalLogin(string username,DateTime start, DateTime end)
        {
            using (var _connection = ConnectionFactory.GetOpenConnection())
            {
                DynamicParameters pars = new DynamicParameters();
                string str = " where Level=@Level and Url=@Url and Logged between @start and @end";
                if (!string.IsNullOrEmpty(username))
                {
                    pars.Add("username", username);
                    str = str + " and username like %@username%";
                }
                pars.Add("start", start, System.Data.DbType.DateTime);
                pars.Add("end", end, System.Data.DbType.DateTime);
                pars.Add("level", "Info");
                pars.Add("Url", "/Account/CheckToken");

                return _connection.QueryFirstOrDefault<int>("select count(*) from NLog " + str, pars);
            }
                
        }

 

对于like语句,拼接%

 

posted @ 2016-11-02 22:16  悠哉大斌  阅读(13192)  评论(1编辑  收藏  举报