我正在处理一个ASP.NET项目的数据库迁移(从Oracle到SQL Server),如果要修改一个连接助手类,这项工作的一部分。
这个类特别包含一段代码,用于在C#变量类型和OracleDbType枚举的成员之间执行匹配。因此,我必须将这种匹配转换为C#变量类型和SqlDbType枚举成员之间的匹配。
我使用的第一种方法是使用以下步骤将OracleDbType的成员转换为SqlDbType的成员:
在Oracle's official documentation中使用此表-Find与OracleDbType成员匹配的Oracle SQL类型
在microsoft's official documentation中使用此表-Find相应的Sql Server数据类型
在microsoft's official documenation中使用此表-Find相应的SqlDbType枚举成员
然而,这种方法给我带来了一些问题。例如,在我的原始代码中,'short‘变量(翻译成OracleDbType.Int16)和'int’变量(翻译成OracleDbType.Int32)是有区别的。使用我上面描述的方法,我必须将OracleDbType.Int16和OracleDbType.Int32都转换成SqlDbType.Decimal,这很奇怪,看起来也不正确。
因此,我选择只使用this table,并将重点放在“.NET框架类型”和“SqlDbType枚举”列来进行转换,这将导致将OracleDbType.Int16转换为SqlDbType.SmallInt,将OracleDbType.Int32转换为SqlDbType.BigInt。
我想知道我的哪种方法是正确的,为什么。
发布于 2019-12-02 20:26:59
这是一个古老的问题,但也许我可以帮助您阐明这一点。您在问题中的一个假设是错误的,即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还可以查看this链接,它也提供了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 }
};希望这能有所帮助。
https://stackoverflow.com/questions/43629014
复制相似问题