如何使用存储过程oracle sql和vb.net填充组合框

时间:2018-05-15 10:36:15

标签: vb.net oracle

我是oracle中的新手,并且正在使用程序并且还在学习,但我有一个问题,我如何使用存储过程填充组合框?我已经有了一个代码,但是当我运行它时,它显示错误IndexOutOfRangedException未处理且无法找到第1列。

这是我的代码 (Procedure / Oracle Sql:)

procedure cmbbox_location (o_output out o_refcur) 
as
    o_cur o_refcur;
begin
    open o_cur for
        select city_id, city_name from city;

    o_output := o_cur;
end cmbbox_location;

(节目/ vb.net)

Private Sub Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    instantiate_dev()
    ora_conn.Open()
    populate_cmbbox_loc()
End Sub

Private Sub populate_cmbbox_loc()
    instantiate_dev()
    ora_conn.Open()
    qr.populate_location()
    cmblocation.DataSource = dt
    cmblocation.ValueMember = dt.Columns(0).ColumnName
    cmblocation.DisplayMember = dt.Columns(1).ColumnName
    cmblocation.Text = ""
    cmblocation.SelectedIndex = 0
End Sub

Public Function populate_location()
    dt = New DataTable
    bg.GetProcDataTable(connStr, "Location_Package.cmbbox_location")
    cmd.Parameters.Add(New OracleParameter("O_OUTPUT", OracleDbType.RefCursor)).Direction = ParameterDirection.Output
    adap_or.SelectCommand = cmd
    adap_or.Fill(dt)

    Return dt
    ora_conn.Close()
End Function

1 个答案:

答案 0 :(得分:0)

您收到的错误表明您正在索引某些不存在的内容,您的存储过程可能/可能无法返回任何内容。解决William Robertson关于引用光标的问题:

有多种方法可以输出引用游标。我最常用的是在包定义中定义的引用游标类型,例如:

CREATE OR REPLACE PACKAGE Location_Package IS

    TYPE DataOutCursor IS REF CURSOR;
    procedure cmbbox_location (o_output out DataOutCursor);

END Location_Package;
/

然后在包体中的程序中:

CREATE OR REPLACE PACKAGE BODY Location_Package IS
    procedure cmbbox_location (o_output out DataOutCursor) as
    begin
        open o_output for
        select city_id, city_name from city;
    end;
END;
/

我认为错误发生在这里:

cmblocation.ValueMember = dt.Columns(0).ColumnName
cmblocation.DisplayMember = dt.Columns(1).ColumnName

最好确保数据表首先包含数据,因此请将组合框行包装为:

IF dt.Rows.Count > 0 Then
    ' ..... assign the data table to the combo box
Else
    ' Some error message ...
End IF

这将消除未处理的异常并帮助您找出问题。

最后,你的函数需要返回一个值(datatable):

Public Function populate_location() as Datatable
    dt = New DataTable
    bg.GetProcDataTable(connStr, "Location_Package.cmbbox_location")
    cmd.Parameters.Add(New OracleParameter("O_OUTPUT", OracleDbType.RefCursor)).Direction = ParameterDirection.Output
    adap_or.SelectCommand = cmd
    adap_or.Fill(dt)

    Return dt
    ora_conn.Close()
End Function