[原]CodeSmith 三层结构实现常用转换方法
//根据字段数据类型转换为C#数据类型:
public string GetCSharpVariableType(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.Guid: return "Guid";
case DbType.SByte: return "sbyte";
case DbType.Single: return "float";
case DbType.Int16: return "short";
case DbType.Int32: return "int";
case DbType.Int64: return "long";
case DbType.Double: return "double";
case DbType.UInt16: return "ushort";
case DbType.UInt32: return "uint";
case DbType.UInt64: return "ulong";
case DbType.AnsiString:
case DbType.String:
case DbType.StringFixedLength:
case DbType.AnsiStringFixedLength: return "string";
case DbType.Binary: return "byte[]";
case DbType.Boolean: return "bool";
case DbType.Byte: return "byte";
case DbType.VarNumeric:
case DbType.Decimal:
case DbType.Currency: return "decimal";
case DbType.Date:
case DbType.DateTime: return "DateTime";
case DbType.Object: return "object";
case DbType.Time: return "TimeSpan";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
//根据字段数据类型及变量名称(str)转换为C#数据类型的表示方法:
public string GetCSharpVariableType(ColumnSchema column, string str)
{
if (column.Name.EndsWith("TypeCode")) return str;
switch (column.DataType)
{
case DbType.AnsiString: return str + ".ToString()";
case DbType.AnsiStringFixedLength: return str + ".ToString()";
case DbType.Binary: return "((" + str + ")==DBNull.Value)?Convert.ToByte(0):Convert.ToByte(" + str + ")";
case DbType.Boolean: return "(bool)" + str;
case DbType.Byte: return "((" + str + ")==DBNull.Value)?Convert.ToByte(0):Convert.ToByte(" + str + ")";
case DbType.Currency: return "((" + str + ")==DBNull.Value)?0:Convert.ToDecimal(" + str + ")";
case DbType.Date: return "((" + str + ")==DBNull.Value)?Convert.ToDateTime(\"1900-1-1\"):Convert.ToDateTime(" + str + ")";
case DbType.DateTime: return "((" + str + ")==DBNull.Value)?Convert.ToDateTime(\"1900-1-1\"):Convert.ToDateTime(" + str + ")";
case DbType.Decimal: return "((" + str + ")==DBNull.Value)?0:Convert.ToDecimal(" + str + ")";
case DbType.Double: return "((" + str + ")==DBNull.Value)?0:Convert.ToDouble(" + str + ")";
case DbType.Guid: return "(Guid)"+ str;
case DbType.Int16: return "(short)" + str;
case DbType.Int32: return "((" + str + ")==DBNull.Value)?0:Convert.ToInt32(" + str + ")";
case DbType.Int64: return "(long)" + str;
case DbType.Object: return "(object)" + str;
case DbType.SByte: return "Convert.ToSByte(" + str + ")";
case DbType.Single: return "(float)" + str;
case DbType.String: return str + ".ToString()";
case DbType.StringFixedLength: return str + ".ToString()";
case DbType.Time: return "(DateTime)" + str;
case DbType.UInt16: return "(ushort)" + str;
case DbType.UInt32: return "(uint)" + str;
case DbType.UInt64: return "(ulong)" + str;
case DbType.VarNumeric: return "Convert.ToDecimal(" + str + ")";
default:
{
return "__UNKNOWN__" + str;
}
}
}
//根据数据字段类型获取该类型在.Net DBType中的表示方法
Ex: DBType.<%= GetCSharpSqlType(column) %>
public string GetCSharpSqlType(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return "";
switch (column.DataType)
{
case DbType.AnsiString: return ".String";
case DbType.AnsiStringFixedLength: return ".String";
case DbType.Binary: return ".Binary";
case DbType.Boolean: return ".Boolean" ;
case DbType.Byte: return ".TinyInt";
case DbType.Currency: return ".Currency";
case DbType.Date: return "";
case DbType.DateTime: return ".DateTime";
case DbType.Decimal: return ".Decimal" ;
case DbType.Double: return ".Float";
case DbType.Guid: return ".Guid";
case DbType.Int16: return ".SmallInt" ;
case DbType.Int32: return ".Int32";
case DbType.Int64: return ".BigInt" ;
case DbType.Object: return "" ;
case DbType.SByte: return "" ;
case DbType.Single: return ".Float" ;
case DbType.String: return ".String";
case DbType.StringFixedLength: return ".String";
case DbType.Time: return ".DateTime" ;
case DbType.UInt16: return "." ;
case DbType.UInt32: return "";
case DbType.UInt64: return "";
case DbType.VarNumeric: return "" ;
default:
{
return "__UNKNOWN__" ;
}
}
}
持续增加中...