CodeSmith部分类型转换代码

View Code
//获取C#数据类型

public string GetCSharpType(ColumnSchema column)

{

    if (column.Name.EndsWith("TypeCode")) return column.Name;

    

    switch (column.DataType)

    {

        case DbType.AnsiString: return "string";

        case DbType.AnsiStringFixedLength: return "string";

        case DbType.Binary: return "byte[]";

        case DbType.Boolean: return "bool";

        case DbType.Byte: return "int";

        case DbType.Currency: return "decimal";

        case DbType.Date: return "DateTime";

        case DbType.DateTime: return "DateTime";

        case DbType.Decimal: return "decimal";

        case DbType.Double: return "double";

        case DbType.Guid: return "Guid";

        case DbType.Int16: return "short";

        case DbType.Int32: return "int";

        case DbType.Int64: return "long";

        case DbType.Object: return "object";

        case DbType.SByte: return "sbyte";

        case DbType.Single: return "float";

        case DbType.String: return "string";

        case DbType.StringFixedLength: return "string";

        case DbType.Time: return "TimeSpan";

        case DbType.UInt16: return "ushort";

        case DbType.UInt32: return "uint";

        case DbType.UInt64: return "ulong";

        case DbType.VarNumeric: return "decimal";

        default:

        {

            return "__UNKNOWN__" + column.NativeType;

        }

    }

}



//获取转换类型

public string GetConvert(ColumnSchema column)

{

    if (column.Name.EndsWith("TypeCode")) return column.Name;

    

    switch (column.DataType)

    {

        case DbType.AnsiString: return "Convert.ToString";

        case DbType.AnsiStringFixedLength: return "Convert.ToString";

        case DbType.Binary: return "Convert.ToByte";

        case DbType.Boolean: return "Convert.ToBoolean";

        case DbType.Byte: return "Convert.ToInt32";

        case DbType.Currency: return "Convert.ToDecimal";

        case DbType.Date: return "Convert.ToDateTime";

        case DbType.DateTime: return "Convert.ToDateTime";

        case DbType.Decimal: return "Convert.ToDecimal";

        case DbType.Double: return "Convert.ToDouble";

        case DbType.Guid: return "Convert.ToString";

        case DbType.Int16: return "Convert.ToInt16";

        case DbType.Int32: return "Convert.ToInt32";

        case DbType.Int64: return "Convert.ToInt64";

        case DbType.Object: return "Convert.ToString";

        case DbType.SByte: return "Convert.ToByte";

        case DbType.Single: return "Convert.ToInt32";

        case DbType.String: return "Convert.ToString";

        case DbType.StringFixedLength: return "Convert.ToString";

        case DbType.Time: return "Convert.DateTime";

        case DbType.UInt16: return "Convert.ToUInt16";

        case DbType.UInt32: return "Convert.ToUInt32";

        case DbType.UInt64: return "Convert.ToUInt64";

        case DbType.VarNumeric: return "Convert.ToDecimal";

        default:

        {

            return "__UNKNOWN__" + column.NativeType;

        }

    }

}



public string GetMySqlDbType(ColumnSchema column)

{

    switch(GetSqlDbType(column))

    {

        case "double":

        return "Double";

        

        case "Bit" :

        return "Int32";

        

        case "Int" :

        return "Int32";

        

        case "BigInt" :

        return "Int64";

        

        case "SmallInt":

        return "Int16";

        

        case "TinyInt" :

        return "Int16";

        

        case "UniqueIdentifier" :

        return "VarChar, 36";

        

        case "NVarChar" :

        if(column.Size > 0)

        {

            return "VarChar, " + column.Size;

        }

        else

        {

            return "Text";

        }

        

        case "VarChar" :

        return "VarChar, " + column.Size;

        

        case "NChar" :

        return "VarChar, " + column.Size;

        

        case "Char" :

        return "VarChar, " + column.Size;

        

        case "Text" :

        return "Text";

        

        case "NText" :

        return "Text";

        

        case "Image" :

        return "LongBlob";

        

        case "DateTime" :

        return "DateTime";

        

        case "SmallDateTime":

        return "DateTime";

    }

    

    return GetSqlDbType(column);

}

你可能感兴趣的:(code)