ODP.NET为SELECT语句实现Cursor

时间:2016-03-20 12:11:16

标签: c# .net oracle-sqldeveloper odp.net odp

如何使用ODP.NET在基本SELECT statement like 'SELECT * FROM Employees'上实现简单的游标提取?

1 个答案:

答案 0 :(得分:1)

所以它很简单。

首先像这样创建OracleConnection

OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);

con.Open();  //opens connection

然后首先通过将原始查询/存储过程作为第一个参数传递来定义和OracleCommand实例,如

因此,在您的特定情况下,它将是OracleCommand cmd = new OracleCommand("SELECT * FROM Employees", con

if (con.State == ConnectionState.Open)
{
using (OracleCommand cmd = new OracleCommand(<query>/<stored proc>, con))
{    
cmd.CommandType = CommandType.StoredProcedure;  //in case of stored proc
cmd.BindByName = true;

OracleDataReader reader;
try
  {
    reader = cmd.ExecuteReader();    
    while(reader.Read())
    {
        Console.WriteLine("field: {0}", reader.GetDecimal(0));  
    }    
  }
catch (OracleException e)
  {
  foreach (OracleError err in e.Errors)
    {
       //print errors         
     }
   }
 con.Close(); 
 con.Dispose();
}
}

以下是示例http://www.oracle.com/technetwork/articles/dotnet/williams-refcursors-092375.html