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

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - bluce chen

swif debounce实现 oculus quest2的思考 博文阅读密码验证 - 博客园 用户中心 - 博客园 https点对点转发响应示意图 Share架构的一些心得 flutter输入颜色枚举卡顿假死 n2n网络环境搭建 p2p技术之n2n源码核心简单分析一 分享一个14年写的用户管理类-swift版 MIUI通过xposed自动设置root权限 基于xposed实现android注册系统服务,解决跨进程共享数据问题 2017 UICollectionView swift2模版 angularjs 分页精华代码 Reveal分析IOS界面,plist文件读取 php嵌套数组递归搜索返回数组key 结合阿里云服务器,设置家中jetson tk1随时远程登陆 prism4 StockTrader RI 项目分析一些体会2
sqlite3 根据实体自动生成建表语句
bluce chen · 2014-04-21 · via 博客园 - bluce chen
 
public class BuildSqlTool
    {
        public static string GetCreateTableSql(object t)
        {
            //CREATE TABLE "StructureView2"("Id" INTEGER PRIMARY KEY  AUTOINCREMENT , "IdxNo" VARCHAR, "Name" VARCHAR, "Content" VARCHAR, "Price" FLOAT, "Order" INTEGER)
            string sqlStr = "CREATE TABLE \"{0}\" ({1})";
            Type type = t.GetType(); 
            PropertyInfo[] pi = type.GetProperties();
            string sqlFormat = "\"{0}\" {1}";
            string sqlStr2 = "";
            foreach (PropertyInfo p in pi)
            {
               string name=p.Name;
               object[]abc=p.GetCustomAttributes(true);
               if (name.ToLower()=="id")
               {
                   sqlStr2 += string.Format(sqlFormat, "Id", "INTEGER PRIMARY KEY  AUTOINCREMENT");
               }
               else
               {
                   sqlStr2 +=","+string.Format(sqlFormat, name, SqlType(p));
               } 
            }
            return string.Format(sqlStr, GetTableName(type), sqlStr2);
        }

        static string GetTableName(Type type)
        {
            var tableAttr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault();
            return tableAttr != null ? tableAttr.Name : type.Name;
        }

        static string SqlType(PropertyInfo p)
        {
            Type clrType = p.PropertyType;
            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32))
            {
                return "integer";
            }
            else if (clrType == typeof(UInt32) || clrType == typeof(Int64))
            {
                return "bigint";
            }
            else if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal))
            {
                return "float";
            }
            else if (clrType == typeof(String))
            {
                return "varchar";
                //int len = MaxStringLength(p);
                //return "varchar(" + len + ")";
            }
            else if (clrType == typeof(DateTime))
            {
                return "datetime"; 
            }
            else if (clrType.IsEnum)
            { 
                return "integer";
            }
            else if (clrType == typeof(byte[]))
            {
                return "blob";
            }
            else if (clrType == typeof(Guid))
            {
                return "varchar(36)";
            }
            else
            {
                throw new NotSupportedException("Don't know about " + clrType);
            }
        }
         
        int MaxStringLength(PropertyInfo p)
        {
            var attrs = p.GetCustomAttributes(typeof(MaxLengthAttribute), true);
            if (attrs.Length > 0)
            {
                return ((MaxLengthAttribute)attrs[0]).Value;
            }
            else
            {
                return 140;
            }
        }
    }

主要目的是为了减少见表,同理,可自动生成orm的sql

使用范例

// 获取当前程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 创建类的实例,返回为 object 类型类的完全限定名(即包括命名空间)
object obj = assembly.CreateInstance("ConstructionBudget.Model.VModel1");
string sql = BuildSqlTool.GetCreateTableSql(obj);