前回(その4)は、REF CURSORをOUT引数として持つストアドプロシージャをVB.NETでDataReaderに受け取る方法を紹介しました。
今回は、REF CURSOR(OUT引数)をVB.NETでDataSetに受け取る方法を紹介します。
開発言語:VB.NET 2005
使用するテーブルは、Oracleを使用している人であればおなじみの、scott/tigerのEMP表です。
ストアドプロシージャは下記の通りです。もらった引数をINSERT文で書き込み、書き込み後のEMP表総件数をout_TOTALCNTにセットします。
(例によって深い意味はありません。説明用です。)
ストアドプロシージャ4
CREATE OR REPLACE PACKAGE SCOTT.PKG_TEST2
IS
TYPE ref_cursor IS REF CURSOR;
Procedure PROCEDURE5(out_cursor OUT ref_cursor);
END;
/
CREATE OR REPLACE PACKAGE BODY SCOTT.PKG_TEST2 AS
Procedure PROCEDURE5 (
out_cursor OUT ref_cursor
)
IS
BEGIN
BEGIN
OPEN out_cursor FOR
SELECT * FROM EMP;
END;
END PROCEDURE5;
END PKG_TEST2;
/
以下、ODP.NET, MicorosoftのOracleClient, OleDB の3種類を使用してPROCEDURE5を呼び出す例です。
今回は、ストアドプロシージャのOUTパラメータをVB.NETのDataSetに受け取る方法について説明します。
まず、OracleParameterのインスタンスを作成します。(OleDBの場合は作成しません。)
OracleParameterのインスタンス作成時の第1引数には、ストアドプロシージャのREF CUROSOR(OUT引数)名(この場合は"out_cursor")、第2引数にはストアドプロシージャOUT引数の型に合わせたものを指定します。(今回の場合、OracleのREF
CURSOR型に対して、ODP.NETの場合はOracleDbType.RefCursor、OracleClientの場合はOracleType.Cursor)
次に、PrameterDirectionにOutputを指定します。(これはストアドプロシージャout_cursorがOUT引数であるため)
あとはDataAdaptertとDataSetを作成し、DataAdapterのFillメソッドを使用することで、DataSetに結果を受け取ることができます。
■ODP.NETサンプル
Try
Using OraConn As New OracleConnection("user id=scott;password=tiger;data source=YourServer")
Using Cmd As New OracleCommand
OraConn.Open()
Cmd.Connection = OraConn
Cmd.CommandType = CommandType.StoredProcedure
Cmd.CommandText = "PKG_TEST2.PROCEDURE5"
Dim outPara As New OracleParameter("out_CURSOR", OracleDbType.RefCursor)
outPara.Direction = ParameterDirection.Output
Cmd.Parameters.Add(outPara)
Dim OraDA As New OracleDataAdapter(Cmd)
Dim ds As New DataSet()
OraDA.Fill(ds, "EMP")
DataGridView1.DataSource = ds.Tables("EMP")
MessageBox.Show("PROCEDURE5 正常終了", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Using
End Using
Catch OraEx As OracleException
MessageBox.Show(OraEx.Message, "ORACLE ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
■OracleClientサンプル
Private Sub OracleClient_TEST5()
Try
Using OraConn As New OracleConnection("user id=scott;password=tiger;data source=YourServer")
Using Cmd As New OracleCommand
OraConn.Open()
Cmd.Connection = OraConn
Cmd.CommandType = CommandType.StoredProcedure
Cmd.CommandText = "PKG_TEST2.PROCEDURE5"
Dim outPara As New OracleParameter("out_CURSOR", OracleType.Cursor)
outPara.Direction = ParameterDirection.Output
Cmd.Parameters.Add(outPara)
Dim OraDA As New OracleDataAdapter(Cmd)
Dim ds As New DataSet()
OraDA.Fill(ds, "EMP")
DataGridView1.DataSource = ds.Tables("EMP")
MessageBox.Show("PROCEDURE5 正常終了", "SUCCESS", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Using
End Using
Catch OraEx As OracleException
MessageBox.Show(OraEx.Message, "ORACLE ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
End Sub
■OleDBサンプル
Private Sub OleDB_TEST5()
Try
Using OraConn As New OleDbConnection("Provider=MSDAORA.1.Oracle;user id=scott;password=tiger;data source=YourServer;PLSQLRset=True")
Using Cmd As New OleDbCommand
OraConn.Open()
Cmd.Connection = OraConn
Cmd.CommandType = CommandType.StoredProcedure
Cmd.CommandText = "PKG_TEST2.PROCEDURE5"
Dim OleDA As New OleDbDataAdapter(Cmd)
Dim ds As New DataSet()
OleDA.Fill(ds, "EMP")
DataGridView1.DataSource = ds.Tables("EMP")
MessageBox.Show("PROCEDURE5 正常終了", "SUCCESS", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Using
End Using
Catch OleEx As OleDbException
MessageBox.Show(OleEx.Message, "OleDB ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
End Sub