T4模版生成实体(二)

本文介绍了如何将Mysql和SQLServer数据库的列信息转换为程序中对应的类型,包括获取表列集合与数据表信息,并展示了ColumnInfo类的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

4. 将数据库类型转为程序类型

        #region GetColumnInfoList 【获取数据库指定表的列集合】
        /// <summary>
        /// 获取数据库指定表的列集合
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public List<ColumnInfo> GetColumnInfoList(string tableName)
        {
            return DatatableToList<ColumnInfo>(GetColumnInfo(tableName));
        }
        #endregion


        #region GetTableInfoList 【获取数据表集合】
        /// <summary>
        /// 获取数据表集合
        /// </summary>
        /// <returns></returns>
        public List<TableInfo> GetTableInfoList()
        {
            return DatatableToList<TableInfo>(GetTableInfo());
        }
        #endregion

实体如下:

    public class TableInfo
    {
        /// <summary>
        /// 表的名称
        /// </summary>
        public string TableName{ get;set;}
        /// <summary>
        /// 表的备注
        /// </summary>
        public string TableRemark { get;set;}
    }

Mysql版本:

public class ColumnInfo
    {
        /// <summary>
        /// 列名
        /// </summary>
        [Column("COLUMN_NAME")]
        public string ColumnName { get; set; }

        /// <summary>
        /// 是否可为空(注意这个顺序要在 DataType 之前,不然再用GetCLRType()时没有值)
        /// </summary>
        [Column("IS_NULLABLE")]
        public string IsNullable { get; set; }

        private string _DataType;
        /// <summary>
        /// 数据类型
        /// </summary>
        [Column("DATA_TYPE")]
        public string DataType { get { return _DataType; } set { _DataType = GetCLRType(value); } }

        /// <summary>
        /// 列备注
        /// </summary>
        [Column("COLUMN_COMMENT")]
        public string ColumnComment { get; set; }



        /// <summary>
        /// 列 键
        /// </summary>
        public string ColumnKey { get; set; }

        public string ColumnDefault { get; set; }

        //[NotMapped]
        //public bool IsPrimary { get; set; }

        private string GetCLRType(string dbType)
        {
            var result = string.Empty;
            switch (dbType)
            {
                case "tinyint":
                case "smallint":
                case "mediumint":
                case "int":
                case "integer":
                    result = "int" + (IsNullable == "YES" ? "?" : "");
                    break;
                case "double":
                    result = "double" + (IsNullable == "YES" ? "?" : "");
                    break;
                case "float":
                    result = "float" + (IsNullable == "YES" ? "?" : "");
                    break;
                case "decimal":
                    result = "decimal" + (IsNullable == "YES" ? "?" : "");
                    break;
                case "numeric":
                case "real":
                    result = "decimal" + (IsNullable == "YES" ? "?" : "");
                    break;
                case "bit":
                    result = "bool" + (IsNullable == "YES" ? "?" : "");
                    break;
                case "date":
                case "time":
                case "year":
                case "datetime":
                case "timestamp":
                    result = "DateTime" + (IsNullable == "YES" ? "?" : "");
                    break;
                case "tinyblob":
                case "blob":
                case "mediumblob":
                case "longblog":
                case "binary":
                case "varbinary":
                    result = "byte[]";
                    break;
                case "char":
                case "varchar":
                case "tinytext":
                case "text":
                case "mediumtext":
                case "longtext":
                case "longblob":
                    result = "string";
                    break;
                case "point":
                case "linestring":
                case "polygon":
                case "geometry":
                case "multipoint":
                case "multilinestring":
                case "multipolygon":
                case "geometrycollection":
                case "enum":
                case "set":
                default:
                    result = dbType;
                    break;
            }
            return result;
        }
    }

SQL SERVER版本:

public class ColumnInfo
	{
		/// <summary>
		/// 列名
		/// </summary>
		[Column("COLUMN_NAME")]
		public string ColumnName { get; set; }

		/// <summary>
		/// 是否可为空(注意这个顺序要在 DataType 之前,不然再用GetCLRType()时没有值)
		/// </summary>
		[Column("IS_NULLABLE")]
		public int IsNullable { get; set; }

		private string _DataType;
		/// <summary>
		/// 数据类型
		/// </summary>
		[Column("DATA_TYPE")]
		public string DataType { get { return _DataType; } set { _DataType = GetCLRType(value); } }

		/// <summary>
		/// 列备注
		/// </summary>
		[Column("COLUMN_COMMENT")]
		public string ColumnComment { get; set; }

		[Column("COLUMN_LENGTH")]
		public int ColumnLength { get; set; }

		/// <summary>
		/// 列 键
		/// </summary>
		public string ColumnKey { get; set; }

		public string ColumnDefault { get; set; }

		//[NotMapped]
		//public bool IsPrimary { get; set; }

		private string GetCLRType(string dbType)
		{
			var result = string.Empty;
			switch (dbType)
			{
				case "uniqueidentifier":
					result = "GUID" + (IsNullable == 1 ? "?" : "");
					break;
				case "tinyint":
				case "smallint":
				case "mediumint":
				case "int":
				case "integer":
					result = "int" + (IsNullable == 1 ? "?" : "");
					break;
				case "double":
					result = "double" + (IsNullable == 1 ? "?" : "");
					break;
				case "float":
					result = "float" + (IsNullable == 1 ? "?" : "");
					break;
				case "decimal":
					result = "decimal" + (IsNullable == 1 ? "?" : "");
					break;
				case "numeric":
				case "real":
					result = "decimal" + (IsNullable == 1 ? "?" : "");
					break;
				case "bit":
					result = "bool" + (IsNullable == 1 ? "?" : "");
					break;
				case "date":
				case "time":
				case "year":
				case "datetime":
				case "timestamp":
					result = "DateTime" + (IsNullable == 1 ? "?" : "");
					break;
				case "tinyblob":
				case "blob":
				case "mediumblob":
				case "longblog":
				case "binary":
				case "varbinary":
					result = "byte[]";
					break;
				case "char":
				case "varchar":
				case "nvarchar":
				case "tinytext":
				case "text":
				case "mediumtext":
				case "longtext":
				case "longblob":
					result = "string";
					break;
				case "point":
				case "linestring":
				case "polygon":
				case "geometry":
				case "multipoint":
				case "multilinestring":
				case "multipolygon":
				case "geometrycollection":
				case "enum":
				case "set":
				default:
					result = dbType;
					break;
			}
			return result;
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值