我正在研究ASP.NET项目(从Oracle到SQL Server)的数据库迁移,如果要修改连接帮助程序类,则需要完成部分工作。

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

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

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

-在microsoft's official documentation中使用此表查找相应的Sql Server数据类型

-在microsoft's official documenation中使用此表查找相应的SqlDbType枚举成员

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

因此,我选择仅使用this table并专注于“ .NET Framework类型”和“ SqlDbType枚举”列来进行转换,这导致将OracleDbType.Int16转换为SqlDbType.SmallInt并将OracleDbType.Int32转换为SqlDbType.BigInt 。

我想知道哪种方法正确,为什么。

最佳答案

这是一个古老的问题,但是也许我可以帮助您对此有所了解。问题中的一个假设是,OracleDbType.Int32是SQLServer中的BigInt。实际上是SqlDbType.Int。 OracleDbType.Int64是SqlDbType.BigInt。因此,如果我必须创建一个Oracle类型到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类型的this链接。

在我看来,我将为您的转换创建一个字典,该字典将OracleDbType链接到SqlDbType等。我在SqlDbType和.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 }
};


希望这可以帮助。

关于c# - 在ASP.NET项目中将数据类型从Oracle转换为SQL Server,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43629014/

10-12 06:29