惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
小众软件
小众软件
I
InfoQ
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Martin Fowler
Martin Fowler
月光博客
月光博客
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
V
Visual Studio Blog
博客园 - 叶小钗
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
GbyAI
GbyAI
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - zeus2

Xfire的初次使用 SQl Server 2012正式版发布 单例模式的三种实现方法 Oracle常用Hint Oracle 设计海量数据库 读书笔记(三) Oracle 设计海量数据库 读书笔记(二) Oracle 设计海量数据库 读书笔记(一) 解决中文ID3标签乱码zz 系统架构性能提高方案! 使用开源工具架设开发平台 修改SQL Server数据库地址 System.DateTimeOffset Load的问题 关于__doPostBack之前截获调用 - zeus2 - 博客园 当应用程序发布到iis7/iis7.5出现需要使用经典模式时 - zeus2 - 博客园 [读书笔记]SQL技术内幕Identity XML序列化封装 生活太艰难了。!!! 从底层角度看ASP.NET-A low-level Look at the ASP.NET Architecture(转载) C++访问Sqlite数据库(存档) - zeus2 - 博客园
根据实体类生成查询安全版
zeus2 · 2009-06-25 · via 博客园 - zeus2

   public static IList<SqlParameter> GetParas<T>(T t, out string where)

    {

        StringBuilder sb = new StringBuilder();

        List<SqlParameter> paras = new List<SqlParameter>();

        Type type = typeof(T);

        foreach (var item in type.GetProperties())

        {

            object value = item.GetValue(t, null);

            if (value == null)

            {

                continue;

            }

            if (value.GetType() == typeof(string))

            {

                string s = "%" + (string)value + "%";

                if (!string.IsNullOrEmpty(s))

                {

                    if (Regex.IsMatch(item.Name, "id", RegexOptions.IgnoreCase))

                    {

                        paras.Add(new SqlParameter(string.Format("@{0}", item.Name), value));

                        sb.AppendFormat(" AND {0} = @{0}", item.Name);

                    }

                    else

                    {

                        paras.Add(new SqlParameter(string.Format("@{0}", item.Name), s));

                        sb.AppendFormat(" AND {0} LIKE @{0}", item.Name);

                    }

                }

            }

            else

            {

                paras.Add(new SqlParameter(string.Format("@{0}", item.Name), value));

                if (Regex.IsMatch(item.Name, "from$", RegexOptions.IgnoreCase))

                {

                    sb.AppendFormat(" AND {0} >= @{0}", item.Name);

                }

                else if (Regex.IsMatch(item.Name, "to$", RegexOptions.IgnoreCase))

                {

                    sb.AppendFormat(" AND {0} <= @{0}", item.Name);

                }

                else

                    sb.AppendFormat(" AND {0} = @{0}", item.Name);

            }

        }

        where = sb.ToString();

        return paras;

    }