在下面的代码中,我使用的是Oracle 11g表达式版本。获取列数据时,如果源为NUMBER(16,3)但绑定数据为SQL_C_DOUBLE,则转换中似乎存在一些错误。
using System;
using System.Data.Odbc;
namespace OracleNumberConversionTestCSharp
{
class Program
{
static void Main(string[] args)
{
OdbcConnection connection = new OdbcConnection();
connection.ConnectionString = "Driver={Oracle in OraClient12Home1}; Dbq=127.0.0.1/XE; Uid=System; Pwd=password;";
connection.Open();
//Drop the table from a previous run
OdbcCommand command = new OdbcCommand("DROP TABLE ORACLE_NUMBER_TEST",connection);
try
{
//would fail if table hasn't been created yet
command.ExecuteNonQuery();
}
catch
{
}
//Create the table
command = new OdbcCommand("CREATE TABLE ORACLE_NUMBER_TEST (MYNUM NUMBER(16,3))",connection);
command.ExecuteNonQuery();
//Insert the value
command = new OdbcCommand("INSERT INTO ORACLE_NUMBER_TEST (MYNUM) values (2014056132407.123)",connection);
command.ExecuteNonQuery();
//Get the value back from the db
command = new OdbcCommand("SELECT MYNUM FROM ORACLE_NUMBER_TEST",connection);
OdbcDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
double expected = 2014056132407.123;
double actual = reader.GetDouble(0);
Console.WriteLine("Actual = " + actual.ToString("R"));
Console.WriteLine("Expected = " + expected.ToString("R"));
}
}
}
}
结果是:
Actual = 2014056132407.12
Expected = 2014056132407.123
使用以下几种方法解决了这个问题:
SELECT CAST (MYNUM AS BINARY_DOUBLE) FROM ORACLE_NUMBER_TEST
double actual = reader.GetDouble(0);
更改为double actual = Convert.ToDouble(reader.GetDecimal(0));
如果我在C中编写示例,我会得到相同的行为.SQL Server和MySQL中的类似SQL会返回预期的结果。这看起来像是Oracle驱动程序中从Oracle NUMBER转换为double的错误,还是在可接受行为的参数范围内?