Read numeric data from oracle table in c#

302 Views Asked by At

I am trying to write c# function to read some data from oracle table

My functions:

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);
    }
}

There is no problem in connection, nor executing query.

When I read data from oracle table in type number(9) it returns proper values I am expecting.

When I read data from table, where type is number(9,2) it returns empty value (like empty table).

Notice: This is only sample of the code. It has to be written using IDb interfaces

Thank you for help

2

There are 2 best solutions below

1
On
3
On
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]+"");
            }
        }
    }
}

You can Try

        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]+"");
            }