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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

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

    }