我正在尝试编写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);
}
}
连接没有问题,也没有执行查询。
当我从类型编号(9)的oracle表中读取数据时,它返回我期望的正确值。
当我从表中读取数据时,类型为数字(9,2),它返回空值(如空表)。
注意:这只是代码的示例。它必须使用IDb接口编写
感谢您的帮助
答案 0 :(得分:0)
答案 1 :(得分: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]+"");
}