2014-08-27 53 views
2

我一直在尝试将DataGridView连接到作为引用游标的存储过程的输出。我搜索了很多网站和博客,无法解决我遇到的问题。 这是我用于创建在Oracle 11g中的程序代码:将我的DataGridView绑定到Ref光标

Create or replace package testpkg 
as 
    type T_cursor is ref cursor; 
    procedure cursor1 (IDp in number, records out T_cursor); 
end testpkg; 
/

create or replace package body testpkg as 
    procedure cursor1 (IDp in number, records out T_cursor) 
    is 
     temp_records T_Cursor; 
    begin 
     open temp_records for 
      select status,at_time 
      from shift_records 
      where employee_id=IDp; 
     records:=temp_records; 
    end cursor1; 
end testpkg; 
/

这是我在C#中使用的代码,我设计方法 - 应用程序在Windows,EmpSal是在DataGridView名称:

DataSet ds = new DataSet(); 
OracleConnection con = new OracleConnection(connString); 
OracleCommand cmd = new OracleCommand(); 

cmd.CommandText = "testpkg.cursor1"; 
cmd.CommandType = CommandType.StoredProcedure; 

cmd.Parameters.Add("IDp", OracleDbType.Int16); 
cmd.Parameters["IDp"].Direction = ParameterDirection.Input; 
cmd.Parameters["IDp"].Value = ID; 

cmd.Parameters.Add("records", OracleDbType.RefCursor); 
cmd.Parameters["records"].Direction = ParameterDirection.Output; 


OracleDataAdapter da = new OracleDataAdapter(cmd); 


DataTable dt = new DataTable(); 
dt.Locale = System.Globalization.CultureInfo.InvariantCulture; 
da.Fill(dt); 

BindingSource bindingSource1 = new BindingSource(); 
bindingSource1.DataSource = dt; 


EmpSal.DataSource = bindingSource1; 

我知道这个C#可能看起来不是很逻辑,这是因为我从很多博客中引入它并对其进行了很多修改。但仍然得到此错误:

InvalidOperationException was unhandled 
Operation is not valid due to the current state of the object. 

此错误在:“da.Fill(dt);”

任何帮助表示赞赏。

回答

1

看起来你并没有打开你的连接对象。 (**在旁注中的OracleConnection是一个过时的类)。

此链接应该给你一个很好的例子,一起工作(对于.NET框架4.5)

http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracleconnection(v=vs.110).aspx

您应该结束了与接近的东西:

using (OracleConnection connection = new OracleConnection(connString)) 
{ 
    DataTable dt = new DataTable(); 
    OracleCommand cmd = new OracleCommand(); 
    cmd.CommandText = "testpkg.cursor1"; 
    cmd.CommandType = CommandType.StoredProcedure; 

    cmd.Parameters.Add("IDp", OracleDbType.Int16); 
    cmd.Parameters["IDp"].Direction = ParameterDirection.Input; 
    cmd.Parameters["IDp"].Value = ID; 

    cmd.Parameters.Add("records", OracleDbType.RefCursor); 
    cmd.Parameters["records"].Direction = ParameterDirection.Output; 
    cmd.Connection = connection; 

    try 
    { 
     connection.Open(); 
     OracleDataAdapter da = new OracleDataAdapter(cmd);   
     da.Fill(dt); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
} 
+0

它工作!非常感谢:) :) – 2014-08-28 13:09:29

+0

当然没问题,很高兴能成为服务。另外...您可能需要编辑/删除帖子中的连接字符串。 ESP。如果它是你在RL中使用的那个。刚注意到这一点。 – jinksPadlock 2014-08-28 16:18:29

+0

完成:D感谢提示我的朋友 – 2014-08-28 17:02:45