-1

我正在尝试编写 c# 函数来从 oracle 表中读取一些数据

我的职能:

public static writeConsole(string query, string connectionString, string driver)
{
    //driver = Oracle.ManagedDataAccess.Client
    using (var conn = DbProviderFactories.GetFactory(driver).CreateConnection())
    {
        using (var cmd = conn.CreateCommand())
        {
            cmd.Connection.ConnectionString = connectionString;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = query;

            foreach (var item in ReadDouble(cmd))
            {
                Console.WriteLine(item);
            }
        }
    }
}
private static IEnumerable<double> ReadDouble(IDbCommand cmd)
{
    using (var r = cmd.ExecuteReader())
    {
        while (r.Read())
            yield return r.GetDouble(0);
    }
}

连接没有问题,也没有执行查询。

当我从 oracle 表中读取 number(9) 类型的数据时,它会返回我期望的正确值。

当我从表中读取数据时,类型为 number(9,2) 它返回空值(如空表)。

注意:这只是代码示例。它必须使用 IDb 接口编写

谢谢你的帮助

4

2 回答 2

0
public static writeConsole(string query, string connectionString, string driver)
{
    //driver = Oracle.ManagedDataAccess.Client
    using (var conn = DbProviderFactories.GetFactory(driver).CreateConnection())
    {
        using (var cmd = conn.CreateCommand())
        {
            cmd.Connection.ConnectionString = connectionString;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = query;
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader[0]+"");
            }
        }
    }
}

你可以试试

        OracleConnection conn = new OracleConnection(connectionString);
        OracleCommand cmd = new OracleCommand(query, conn);
        if (conn.State == ConnectionState.Closed)
            conn.Open();
        OracleDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
            {
                Console.WriteLine(reader[0]+"");
            }
于 2016-06-07T12:44:11.927 回答
0

可能是类型映射的问题。尝试这个:

http://docs.oracle.com/html/E10927_01/featSafeType.htm#i1008428

和这个:

https://community.oracle.com/message/3582080

于 2016-06-07T12:42:33.440 回答