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;
}
}