开发者社区> 问答> 正文

如何在ASP.NET项目中将数据类型从 Oracle 转换为 SQL Server

如何在ASP.NET项目中将数据类型从 Oracle 转换为 SQL Server

展开
收起
贺贺_ 2019-12-02 21:01:10 650 0
1 条回答
写回答
取消 提交回答
  • 这是一个老问题,但也许我可以帮你澄清一下。您的问题中错误的假设之一是 OracleDbType.Int32 是 SQLServer 中的 BigInt。实际上是一个SqlDbType.Int。甲骨文DbType.Int64是一个SqlDbType.BigInt。因此,如果我必须创建一个比较表的甲骨文到框架到SqlServer的类型,我会把它这样

    Oracle          .Net Type       SqlServerType
    BFile           byte[]          varbinary(max) With FileStream
    Blob            byte[]          varbinary
    Byte            byte            tinyint
    Char            Char/String     char
    Clob            String          varchar(max)
    Date            DateTime        Date
    Decimal         decimal         decimal/numeric
    Double          double          float
    Int16           Int16           smallint
    Int32           int             int
    Int64           long/Int64      bigint
    Long            String          (n)varchar(max)
    LongRaw         byte[]          varbinary(max)
    NChar           String          nchar
    NClob           String          nvarchar
    NVarchar2       String          nvarchar
    Raw             byte[]          varbinary
    Single          single          real
    TimeStamp       DateTime        datetime2
    TimeStampLTZ    DateTime        datetimeoffset (i think)
    TimeStampTZ     DateTime        datetimeoffset
    Varchar2        String          varchar
    XmlType         String          XML
    
    

    此外,请查看此链接,该链接还提供 CLR 类型。 在我看来,我会为转换创建一个字典,将 OracleDbType 链接到 SqlDbType 等。我有一个类似的字典,我在 SqlDbTypes 和 .Net 类型之间使用。

    public static Dictionary<Type, SqlDbType> typeMap = 
        new Dictionary<Type, SqlDbType>() 
    { 
        { typeof(byte), SqlDbType.TinyInt}, { typeof(sbyte), SqlDbType.TinyInt },
        { typeof(short), SqlDbType.SmallInt}, { typeof(ushort), SqlDbType.SmallInt },
        { typeof(int), SqlDbType.Int }, {typeof(uint), SqlDbType.Int },
        { typeof(long), SqlDbType.BigInt },   {typeof(ulong), SqlDbType.BigInt },               
        { typeof(float), SqlDbType.Float }, { typeof(double), SqlDbType.Float },
        { typeof(decimal), SqlDbType.Decimal }, {typeof(bool),  SqlDbType.Bit },
        { typeof(string), SqlDbType.VarChar },  {typeof(char), SqlDbType.Char },
        { typeof(Guid),  SqlDbType.UniqueIdentifier }, { typeof(DateTime), SqlDbType.DateTime}, 
        { typeof(DateTimeOffset), SqlDbType.DateTimeOffset }, { typeof(byte[]), SqlDbType.VarBinary },
        //Nullable fields
        { typeof(byte?), SqlDbType.TinyInt }, { typeof(sbyte?), SqlDbType.TinyInt },  
        { typeof(short?), SqlDbType.SmallInt}, { typeof(ushort?), SqlDbType.SmallInt }, 
        { typeof(int?), SqlDbType.Int }, { typeof(uint?), SqlDbType.Int }, 
        { typeof(long?), SqlDbType.BigInt }, { typeof(ulong?), SqlDbType.BigInt },
        { typeof(float?), SqlDbType.Float }, { typeof(double?), SqlDbType.Float },
        { typeof(decimal?), SqlDbType.Decimal}, { typeof(bool?), SqlDbType.Bit },
        { typeof(Guid?), SqlDbType.UniqueIdentifier}, { typeof(DateTime?), SqlDbType.DateTime },
        { typeof(DateTimeOffset?), SqlDbType.DateTimeOffset }
    };
    
    2019-12-02 21:02:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PostgresChina2018_樊文凯_ORACLE数据库和应用异构迁移最佳实践 立即下载
PostgresChina2018_王帅_从Oracle到PostgreSQL的数据迁移 立即下载
Oracle云上最佳实践 立即下载

相关镜像