如何在ASP.NET项目中将数据类型从 Oracle 转换为 SQL Server
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
这是一个老问题,但也许我可以帮你澄清一下。您的问题中错误的假设之一是 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 }
};