• 痛快-代码生成之数据类型和C#类弄转换


    前言

    在代码生成器中,我们用来对数据类型和C#类型转换的映射

    代码实现

    <?xml version="1.0" encoding="utf-8" ?>
    <Languages>
      <Language From="SQL" To="C#">
        <Type From="bigint" To="long" />
        <Type From="binary" To="object" />
        <Type From="bit" To="bool" />
        <Type From="char" To="string" />
        <Type From="datetime" To="DateTime" />
        <Type From="decimal" To="decimal" />
        <Type From="float" To="double" />
        <Type From="image" To="byte[]" />
        <Type From="int" To="int" />
        <Type From="money" To="decimal" />
        <Type From="nchar" To="string" />
        <Type From="ntext" To="string" />
        <Type From="numeric" To="decimal" />
        <Type From="nvarchar" To="string" />
        <Type From="real" To="float" />
        <Type From="smalldatetime" To="DateTime" />
        <Type From="smallint" To="short" />
        <Type From="smallmoney" To="decimal" />
        <Type From="text" To="string" />
        <Type From="timestamp" To="byte[]" />
        <Type From="tinyint" To="byte" />
        <Type From="uniqueidentifier" To="Guid" />
        <Type From="varbinary" To="byte[]" />
        <Type From="varchar" To="string" />
        <Type From="xml" To="string" />
        <Type From="sql_variant" To="object" />
      </Language>
      <Language From="SQL" To="C# System Types">
        <Type From="bigint" To="System.Int64" />
        <Type From="binary" To="System.Object" />
        <Type From="bit" To="System.Boolean" />
        <Type From="char" To="System.String" />
        <Type From="datetime" To="System.DateTime" />
        <Type From="decimal" To="System.Decimal" />
        <Type From="float" To="System.Double" />
        <Type From="image" To="System.Byte[]" />
        <Type From="int" To="System.Int32" />
        <Type From="money" To="System.Decimal" />
        <Type From="nchar" To="System.String" />
        <Type From="ntext" To="System.String" />
        <Type From="numeric" To="System.Decimal" />
        <Type From="nvarchar" To="System.String" />
        <Type From="real" To="System.Single" />
        <Type From="smalldatetime" To="System.DateTime" />
        <Type From="smallint" To="System.Int16" />
        <Type From="smallmoney" To="System.Decimal" />
        <Type From="text" To="System.String" />
        <Type From="timestamp" To="System.Byte[]" />
        <Type From="tinyint" To="System.Byte" />
        <Type From="uniqueidentifier" To="System.Guid" />
        <Type From="varbinary" To="System.Byte[]" />
        <Type From="varchar" To="System.String" />
        <Type From="xml" To="System.String" />
        <Type From="sql_variant" To="System.Object" />
      </Language>
      <DbTarget From="SQL" To="SqlClient">
        <Type From="bigint" To="SqlDbType.BigInt" />
        <Type From="binary" To="SqlDbType.Binary" />
        <Type From="bit" To="SqlDbType.Bit" />
        <Type From="char" To="SqlDbType.Char" />
        <Type From="datetime" To="SqlDbType.DateTime" />
        <Type From="decimal" To="SqlDbType.Decimal" />
        <Type From="float" To="SqlDbType.Float" />
        <Type From="image" To="SqlDbType.Image" />
        <Type From="int" To="SqlDbType.Int" />
        <Type From="money" To="SqlDbType.Money" />
        <Type From="nchar" To="SqlDbType.NChar" />
        <Type From="ntext" To="SqlDbType.NText" />
        <Type From="numeric" To="SqlDbType.Decimal" />
        <Type From="nvarchar" To="SqlDbType.NVarChar" />
        <Type From="real" To="SqlDbType.Real" />
        <Type From="smalldatetime" To="SqlDbType.SmallDateTime" />
        <Type From="smallint" To="SqlDbType.SmallInt" />
        <Type From="smallmoney" To="SqlDbType.SmallMoney" />
        <Type From="text" To="SqlDbType.Text" />
        <Type From="timestamp" To="SqlDbType.Timestamp" />
        <Type From="tinyint" To="SqlDbType.TinyInt" />
        <Type From="uniqueidentifier" To="SqlDbType.UniqueIdentifier" />
        <Type From="varbinary" To="SqlDbType.VarBinary" />
        <Type From="varchar" To="SqlDbType.VarChar" />
        <Type From="xml" To="SqlDbType.Xml" />
        <Type From="sql_variant" To="SqlDbType.Variant" />
      </DbTarget>
      <DbTarget From="SQLCE" To="SqlServerCe">
        <Type From="bigint" To="SqlDbType.BigInt" />
        <Type From="binary" To="SqlDbType.Binary" />
        <Type From="bit" To="SqlDbType.Bit" />
        <Type From="char" To="SqlDbType.Char" />
        <Type From="datetime" To="SqlDbType.DateTime" />
        <Type From="decimal" To="SqlDbType.Decimal" />
        <Type From="float" To="SqlDbType.Float" />
        <Type From="image" To="SqlDbType.Image" />
        <Type From="int" To="SqlDbType.Int" />
        <Type From="money" To="SqlDbType.Money" />
        <Type From="nchar" To="SqlDbType.NChar" />
        <Type From="ntext" To="SqlDbType.NText" />
        <Type From="numeric" To="SqlDbType.Decimal" />
        <Type From="nvarchar" To="SqlDbType.NVarChar" />
        <Type From="real" To="SqlDbType.Real" />
        <Type From="smalldatetime" To="SqlDbType.SmallDateTime" />
        <Type From="smallint" To="SqlDbType.SmallInt" />
        <Type From="smallmoney" To="SqlDbType.SmallMoney" />
        <Type From="text" To="SqlDbType.Text" />
        <Type From="timestamp" To="SqlDbType.Timestamp" />
        <Type From="tinyint" To="SqlDbType.TinyInt" />
        <Type From="uniqueidentifier" To="SqlDbType.UniqueIdentifier" />
        <Type From="varbinary" To="SqlDbType.VarBinary" />
        <Type From="varchar" To="SqlDbType.VarChar" />
        <Type From="xml" To="SqlDbType.Xml" />
        <Type From="sql_variant" To="SqlDbType.Variant" />
      </DbTarget>
    </Languages>

    读取方法

    static Dictionary<string, string> list = new Dictionary<string, string>();
            static void Main(string[] args)
            {
                XElement root = XElement.Load("Languages.xml");
    
                var custs = (from c in root.Elements("DbTarget")
                             where c.Attribute("From").Value.Equals("SQL") && c.Attribute("To").Value.Equals("SqlClient")
                             select c).ToList();
    
                foreach (XElement node in custs.Elements("Type"))
                {
                    list.Add(node.Attribute("From").Value, node.Attribute("To").Value);
                }
    
                Console.ReadKey();
            }
  • 相关阅读:
    TIMESTAMP类型字段在SQL Server和MySQL中的含义和使用
    Redis阻塞诊断基础
    MySQL分区表
    Redis 主从复制
    Redis安全以及备份还原
    Redis物理文件结构
    Redis的Errorlog或者启动日志(错误日志)的配置
    Redis 编译安装
    MySQL自增列锁模式 innodb_autoinc_lock_mode不同参数下性能测试
    SQL Server并发操作单个表时发生在page页面级的死锁
  • 原文地址:https://www.cnblogs.com/tongkuai/p/3234600.html
Copyright © 2020-2023  润新知