zoukankan      html  css  js  c++  java
  • SqlLikeAttribute 特性增加 左、右Like实现

    SqlLikeAttribute 特性原来只实现了全Like,今天增加左、右Like实现

    更新时间:2016-04-30
    /// <summary>
            /// 获取查询条件语句
            /// </summary>
            /// <typeparam name="SearchT">查询实体类型</typeparam>
            /// <param name="searchEntity">查询实体</param>
            /// <param name="sqlParameters">参数列表</param>
            /// <param name="WhereSql">条件语句</param>
            /// <param name="item">属性对象</param>
            private static void GetQueryWhere<SearchT>(SearchT searchEntity, List<MySqlParameter> sqlParameters, StringBuilder WhereSql, PropertyInfo property)
            {
                //验证值是否有效
                if (WhereValueValidate<SearchT>(searchEntity, property))
                {
                    var sqlLikeAttr = property.GetCustomAttribute(typeof(SqlLikeAttribute));
                    if (sqlLikeAttr != null)
                    {
                        var columnName = GetColumnName(property);
                        var proprtyValue = filterLikeRiskStr(property.GetValue(searchEntity));
    
                        if (!string.IsNullOrEmpty(proprtyValue))
                        {
                            //获取特性值
                            var sqlLikeVal = (sqlLikeAttr as SqlLikeAttribute).Value;
                            switch (sqlLikeVal)
                            {
                                case SqlLikeType.LikeAll:
                                    WhereSql.AppendFormat(" AND {0} Like '%{1}%'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                    break;
                                case SqlLikeType.LikeLeft:
                                    WhereSql.AppendFormat(" AND {0} Like '%{1}'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                    break;
                                case SqlLikeType.LikeRight:
                                    WhereSql.AppendFormat(" AND {0} Like '{1}%'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                    break;
                                default:
                                    WhereSql.AppendFormat(" AND {0} Like '%{1}%'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                    break;
                            }
                        }
                    }
                    else if (property.GetCustomAttribute(typeof(GreaterAndEqualAttribute)) != null)
                    {
                        var columnName = GetColumnName(property);
                        if (!string.IsNullOrEmpty(columnName))
                        {
                            var parameterName = string.Format("@{0}", property.Name);
                            WhereSql.AppendFormat(" AND {0} >= {1}", columnName, parameterName);
                            sqlParameters.Add(new MySqlParameter(parameterName, property.GetValue(searchEntity)));
                        }
                    }
                    else if (property.GetCustomAttribute(typeof(LessAndEqualAttribute)) != null)
                    {
                        var columnName = GetColumnName(property);
                        if (!string.IsNullOrEmpty(columnName))
                        {
                            var parameterName = string.Format("@{0}", property.Name);
                            var value = property.GetValue(searchEntity);
                            if (value.GetType().Name.Equals("DateTime"))
                            {
                                WhereSql.AppendFormat(" AND {0} < {1}", columnName, parameterName);
                                sqlParameters.Add(new MySqlParameter(parameterName, Convert.ToDateTime(value).AddDays(1)));
                            }
                            else
                            {
                                WhereSql.AppendFormat(" AND {0} <= {1}", columnName, parameterName);
                                sqlParameters.Add(new MySqlParameter(parameterName, value));
                            }
                        }
                    }
                    else
                    {
                        //新方法灵活性高
                        var columnName = GetColumnName(property);
                        if (!string.IsNullOrEmpty(columnName))
                        {
                            var parameterName = string.Format("@{0}", property.Name);
                            WhereSql.AppendFormat(" AND {0} = {1}", columnName, parameterName);
                            sqlParameters.Add(new MySqlParameter(parameterName, property.GetValue(searchEntity)));
                        }                    
                    }
                }
            }
  • 相关阅读:
    C语言枚举类型(C语言enum用法)详解
    基础练习 报时助手
    怎么将CAD转JPG?教你两种CAD转JPG方法
    CAD图纸怎么看?这两种方法值得看
    某酒店建筑设计CAD施工图
    CAD怎么算面积?这种方法你要知道
    怎么将DWG转PDF?分享一个在线转换方法
    如何使用CAD删除命令?怎么删除图纸中线段
    CAD制图软件哪个好?试试这两个就知道了
    CAD制图初学入门如何学好CAD?CAD大神总结5点诀窍,必须收藏
  • 原文地址:https://www.cnblogs.com/stevenchen2016/p/5449691.html
Copyright © 2011-2022 走看看