当前位置:
首页 > Python基础教程 >
-
C#教程之c# 利用t4模板,自动生成Model类(2)
varchar": csharpType = "string"; break;
case "xml": csharpType = "string"; break;
default: csharpType = "object"; break;
}
return csharpType;
}
public static string MapDBType(string dbtype)
{
if (string.IsNullOrEmpty(dbtype)) return dbtype;
dbtype = dbtype.ToLower();
string csharpType = "object";
switch (dbtype)
{
case "bigint": csharpType = "DbType.Int64"; break;
case "binary": csharpType = "DbType.Binary"; break;
case "bit": csharpType = "DbType.Boolean"; break;
case "char": csharpType = "DbType.String"; break;
case "date": csharpType = "DbType.DateTime"; break;
case "datetime": csharpType = "DbType.DateTime"; break;
case "datetime2": csharpType = "DbType.DateTime2"; break;
case "datetimeoffset": csharpType = "DbType.DateTimeOffset"; break;
case "decimal": csharpType = "DbType.Decimal"; break;
case "float": csharpType = "DbType.Double"; break;
case "image": csharpType = "DbType.Binary"; break;
case "int": csharpType = "DbType.Int32"; break;
case "money": csharpType = "DbType.Currency"; break;
case "nchar": csharpType = "DbType.StringFixedLength"; break;
case "ntext": csharpType = "DbType.String"; break;
case "numeric": csharpType = "DbType.Decimal"; break;
case "nvarchar": csharpType = "DbType.String"; break;
case "real": csharpType = "DbType.Single"; break;
case "smalldatetime": csharpType = "DbType.DateTime"; break;
case "smallint": csharpType = "DbType.Int16"; break;
case "smallmoney": csharpType = "DbType.Decimal"; break;
case "sql_variant": csharpType = "DbType.Object"; break;
case "sysname": csharpType = "DbType.Object"; break;
case "text": csharpType = "DbType.String"; break;
case "time": csharpType = "DbType.DateTime"; break;
case "timestamp": csharpType = "DbType.Binary"; break;
case "tinyint": csharpType = "DbType.Byte"; break;
case "uniqueidentifier": csharpType = "DbType.Guid"; break;
case "varbinary": csharpType = "DbType.Binary"; break;
case "varchar": csharpType = "DbType.AnsiString"; break;
case "xml": csharpType = "DbType.Xml"; break;
default: csharpType = "DbType.Object"; break;
}
return csharpType;
}
public static Type MapCommonType(string dbtype)
{
if (string.IsNullOrEmpty(dbtype)) return Type.Missing.GetType();
dbtype = dbtype.ToLower();
Type commonType = typeof(object);
switch (dbtype)
{
case "bigint": commonType = typeof(long); break;
case "binary": commonType = typeof(byte[]); break;
case "bit": commonType = typeof(bool); break;
case "char": commonType = typeof(string); break;
case "date": commonType = typeof(DateTime); break;
case "datetime": commonType = typeof(DateTime); break;
case "datetime2": commonType = typeof(DateTime); break;
case "datetimeoffset": commonType = typeof(DateTimeOffset); break;
case "decimal": commonType = typeof(decimal); break;
case "float": commonType = typeof(double); break;
case "image": commonType = typeof(byte[]); break;
case "int": commonType = typeof(int); break;
case "money": commonType = typeof(decimal); break;
case "nchar": commonType = typeof(string); break;
case "ntext": commonType = typeof(string); break;
case "numeric": commonType = typeof(decimal); break;
case "nvarchar": commonType = typeof(string); break;
case "real": commonType = typeof(Single); break;
case "smalldatetime": commonType = typeof(DateTime); break;
case "smallint": commonType = typeof(short); break;
case "smallmoney": commonType = typeof(decimal); break;
case "sql_variant": commonType = typeof(object); break;
case "sysname": commonType = typeof(object); break;
case "text": commonType = typeof(string); break;
case "time": commonType = typeof(TimeSpan); break;
case "timestamp": commonType = typeof(byte[]); break;
case "tinyint": commonType = typeof(byte); break;
case "uniqueidentifier": commonType = typeof(Guid); break;
case "varbinary": commonType = typeof(byte[]); break;
case "varchar": commonType = typeof(string); break;
case "xml": commonType = typeof(string); break;
default: commonType = typeof(object); break;
}
return commonType;
}
}
#endregion
#>
t4文件保存后,会自动生成类文件
这样,我们省去了很多代码量。