#region GetColumnInfoList 【获取数据库指定表的列聚集】 /// /// 获取数据库指定表的列聚集 /// /// /// public List GetColumnInfoList(string tableName) { return DatatableToList(GetColumnInfo(tableName)); } #endregion #region GetTableInfoList 【获取数据表聚集】 /// /// 获取数据表聚集 /// /// public List GetTableInfoList() { return DatatableToList(GetTableInfo()); } #endregion
复制代码
实体如下:
public class TableInfo { /// /// 表的名称 /// public string TableName{ get;set;} /// /// 表的备注 /// public string TableRemark { get;set;} }
复制代码
Mysql版本:
public class ColumnInfo { /// /// 列名 /// [Column("COLUMN_NAME")] public string ColumnName { get; set; } /// /// 是否可为空(注意这个顺序要在 DataType 之前,否则再用GetCLRType()时没有值) /// [Column("IS_NULLABLE")] public string IsNullable { get; set; } private string _DataType; /// /// 数据范例 /// [Column("DATA_TYPE")] public string DataType { get { return _DataType; } set { _DataType = GetCLRType(value); } } /// /// 列备注 /// [Column("COLUMN_COMMENT")] public string ColumnComment { get; set; } /// /// 列 键 /// 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 { /// /// 列名 /// [Column("COLUMN_NAME")] public string ColumnName { get; set; } /// /// 是否可为空(注意这个顺序要在 DataType 之前,否则再用GetCLRType()时没有值) /// [Column("IS_NULLABLE")] public int IsNullable { get; set; } private string _DataType; /// /// 数据范例 /// [Column("DATA_TYPE")] public string DataType { get { return _DataType; } set { _DataType = GetCLRType(value); } } /// /// 列备注 /// [Column("COLUMN_COMMENT")] public string ColumnComment { get; set; } [Column("COLUMN_LENGTH")] public int ColumnLength { get; set; } /// /// 列 键 /// 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; } }