2014-08-27 50 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