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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Latest news
Latest news
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
博客园 - Franky
博客园_首页
爱范儿
爱范儿
博客园 - 聂微东
月光博客
月光博客
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
IT之家
IT之家
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
Scott Helme
Scott Helme
The Hacker News
The Hacker News
Jina AI
Jina AI
V
Visual Studio Blog
小众软件
小众软件
WordPress大学
WordPress大学
N
News and Events Feed by Topic
L
Lohrmann on Cybersecurity
P
Privacy International News Feed
美团技术团队
D
Darknet – Hacking Tools, Hacker News & Cyber Security
宝玉的分享
宝玉的分享
Spread Privacy
Spread Privacy
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
雷峰网
雷峰网
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
N
News and Events Feed by Topic

博客园 - 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;

    }