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

C# 在ASP.NET项目中将数据类型从Oracle转换为SQL Server,c#,asp.net,sql-server,oracle,C#,Asp.net,Sql Server,Oracle,我正在进行ASP.NET项目(从Oracle到SQL Server)的数据库迁移,如果要修改连接帮助器类,这是我工作的一部分 该类特别包含一段代码,用于在C#变量类型和OracleDbType枚举的成员之间执行匹配。因此,我必须将此匹配转换为C#变量类型和SqlDbType枚举成员之间的匹配 我使用的第一种方法是使用以下步骤将OracleDbType的成员转换为SqlDbType的成员: -在中使用此表查找与OracleDbType成员匹配的Oracle SQL类型 -在中使用此表查找相应的Sq

我正在进行ASP.NET项目(从Oracle到SQL Server)的数据库迁移,如果要修改连接帮助器类,这是我工作的一部分

该类特别包含一段代码,用于在C#变量类型和OracleDbType枚举的成员之间执行匹配。因此,我必须将此匹配转换为C#变量类型和SqlDbType枚举成员之间的匹配

我使用的第一种方法是使用以下步骤将OracleDbType的成员转换为SqlDbType的成员:

-在中使用此表查找与OracleDbType成员匹配的Oracle SQL类型

-在中使用此表查找相应的Sql Server数据类型

-在中使用此表查找相应的SqlDbType枚举成员

然而,这种方法给我带来了一些问题。例如,在我的原始代码中,“short”变量(翻译成OracleDbType.Int16)和“int”变量(翻译成OracleDbType.Int32)之间有区别。使用上面描述的方法,我必须将OracleDbType.Int16和OracleDbType.Int32都转换为SqlDbType.Decimal,这很奇怪,而且似乎不正确

因此,我选择只使用“.NET Framework type”和“SqlDbType enumeration”列进行转换,从而将OracleDbType.Int16转换为SqlDbType.SmallInt,将OracleDbType.Int32转换为SqlDbType.BigInt


我想知道我的方法中哪一种是正确的,为什么是正确的。

这是一个老问题,但也许我可以帮你解释一下。您在问题中的一个错误假设是OracleDbType.Int32在SQLServer中是BigInt。实际上是一个SqlDbType.Int。OracleDbType.Int64是一个SqlDbType.BigInt。所以,如果我必须创建一个Oracle to Framework和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 }
};
公共静态字典类型映射=
新字典()
{ 
{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},
//可空字段
{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}
};
希望这有帮助