HIRO's.NET Blog

PowerShellが好き

目次

Blog 利用状況

ニュース

あわせて読みたい

書庫

日記カテゴリ

リンク

VB.NETからOracleストアドファンクションを呼び出す

前回までは、OracleのストアドプロシージャをVB.NETから使用する方法を紹介しました。

今回は、OracleのストアドファンクションをVB.NETから呼び出す方法を紹介します。

開発言語:VB.NET 2005

使用するテーブルは、Oracleを使用している人であればおなじみの、scott/tigerのEMP表です。

ストアドファンクションは下記の通りです。呼び出すと777と言う数値を返すだけのものです。

(例によって深い意味はありません。説明用です。)

ストアドファンクション1
CREATE OR REPLACE FUNCTION SCOTT.FUNCTION1
RETURN NUMBER -- 戻り値のデータ型
IS
BEGIN
    return (777);
END;
/
/

 以下、ODP.NET, MicorosoftのOracleClient, OleDB の3種類を使用してFUNCTION1を呼び出す例です。

 

 今回は、ストアドファンクションの戻り値をVB.NETで受け取る方法について説明します。

 まず、OracleParameterのインスタンスを作成します。

 OracleParameterのインスタンス作成時の第1引数には、適当に名前を付けた値を指定します。(今回は戻り値を受け取るという意味合いをこめて"retVal"としています)。第2引数にはストアドファンクションの戻り値であるNumber型に対応する型を指定します。(ODP.NETの場合はOracleDbType.Decimal、OracleClientの場合はOracleType.Number、OleDbの場合はOleDbType.Decimal)

 次に、ExecuteNonQueryを実行し、作成しておいたOracleParameterのインスタンス(retVal)のValueプロパティを参照することで、ストアドファンクションの戻り値を取得することができます。

 

■ODP.NETサンプル
Private Sub ODP_FUNCTION1()
    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 = "FUNCTION1"
                'ストアドファンクションの戻り値パラメータ作成
                Dim retVal As New OracleParameter("retVal", OracleDbType.Decimal)
                retVal.Direction = ParameterDirection.ReturnValue
                Cmd.Parameters.Add(retVal)

                'ストアドファンクションの実行
                Cmd.ExecuteNonQuery()

                MessageBox.Show("FUNCTION1 正常終了 戻り値=" & retVal.Value.ToString(), _
                    "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Using
        End Using

    Catch OraEx As OracleException
        'Oracle例外発生時
        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

■OracleClientサンプル
Private Sub OracleClient_FUNCTION1()
    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 = "FUNCTION1"
                'ストアドファンクションの戻り値パラメータ作成
                Dim retVal As New OracleParameter("retVal", OracleType.Number)
                retVal.Direction = ParameterDirection.ReturnValue
                Cmd.Parameters.Add(retVal)

                'ストアドファンクションの実行
                Cmd.ExecuteNonQuery()

                MessageBox.Show("FUNCTION1 正常終了 戻り値=" & retVal.Value.ToString(), _
                    "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Using
        End Using

    Catch OraEx As OracleException
        'Oracle例外発生時
        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_FUNCTION1()
    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 = "FUNCTION1"
                'ストアドファンクションの戻り値パラメータ作成
                Dim retVal As New OleDbParameter("retVal", OleDbType.Decimal)
                retVal.Direction = ParameterDirection.ReturnValue
                Cmd.Parameters.Add(retVal)

                'ストアドファンクションの実行
                Cmd.ExecuteNonQuery()

                MessageBox.Show("FUNCTION1 正常終了 戻り値=" & retVal.Value.ToString(), _
                    "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End Using
        End Using

    Catch OleEx As OleDbException
        'Oracle例外発生時
        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


投稿日時 : 2007年4月22日 17:39

コメントを追加

# Dnotf Qyd Dmhl Tkp Alsvgs 2014/11/30 21:40 Robertwat

www.hrea.com/imgs/sitemap.xml
www.aministorage.biz/imgs/20141126933132969.html
www.newtonsheriffga.org/imgs/201411269424996522.html
www.tophatpartydesign.com/imgs/201411269561269671.html
http://www.mclynns.com/imgs/201411269285812914.html
www.mesothel.com/imgs/201411269342737124.html
www.crompton-canadaeast.com/imgs/201411269522023916.html
www.thetuscankitchen.com/imgs/20141126944374342.html
www.hrea.com/imgs/201411269333147572.html
www.rotateblack.com/imgs/201411269373189445.html
www.howardpriceturf.com/imgs/201411269412081420.html
www.newtonsheriffga.org/imgs/201411269431836493.html
http://www.pk-products.com/imgs/201411269321887108.html
www.mchaleenergy.com/imgs/201411269362175080.html
www.seattlebindery.com/imgs/201411269493868927.html

# Eoghj Frs Fcyi Ybc Bjnrjc 2014/12/04 12:12 MichaelLum

?縣ttp://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/index.html
http://www.lindahlbeck.com/22/Samantha/20141202232139-_.-90zd.html
http://www.lindahlbeck.com/22/Samantha/20141202232139-~~-86io.html
http://www.lindahlbeck.com/22/Samantha/20141202232139-@--20ol.html
http://www.lindahlbeck.com/22/Samantha/20141202232139-.@-83qn.html
http://www.lindahlbeck.com/22/Samantha/20141202232139-~+-05oe.html
http://www.lindahlbeck.com/22/Samantha/20141202232139-$@-08rf.html
http://www.lindahlbeck.com/22/Samantha/20141202232139--.-67ff.html
http://www.lindahlbeck.com/22/Samantha/20141202232139-_--57dr.html
http://www.lindahlbeck.com/22/Samantha/20141202232144-@_-66kx.html
http://www.lindahlbeck.com/22/Samantha/20141202232144-++-85zt.html
http://www.lindahlbeck.com/22/Samantha/20141202232144-~--56xa.html
http://www.lindahlbeck.com/22/Samantha/20141202232145--.-96id.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-@--24ar.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-_.-73rx.html
http://www.lindahlbeck.com/22/Samantha/20141202232145--~-20dr.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-=.-81mq.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-$_-27dj.html
http://www.lindahlbeck.com/22/Samantha/20141202232145--&-11bv.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.--66xk.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-+=-17ga.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-@=-06ke.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-_=-15xr.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-~--24li.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-+@-93ii.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-+@-56mr.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-@_-90up.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-+$-10eo.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-&~-29qu.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-+.-72su.html
http://www.lindahlbeck.com/22/Samantha/20141202232145--.-69ay.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-@+-07zj.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.@-66hd.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-==-47go.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.&-30nh.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-@+-91ie.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-~@-75oa.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-=&-21nr.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-==-18io.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-&$-18dk.html
http://www.lindahlbeck.com/22/Samantha/20141202232145--_-10vf.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-$=-43dk.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-~_-95pt.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.&-44jl.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-~~-54wu.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-@.-97xv.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-+=-21rc.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.+-25tt.html
http://www.lindahlbeck.com/22/Samantha/20141202232145--=-35mv.html
http://www.lindahlbeck.com/22/Samantha/20141202232145--$-30rl.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-&+-07ys.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.-95dz.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-&+-76pw.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-=--13vp.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.&-56oj.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-.@-54vc.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-&=-15nq.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-_+-87pa.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-$.-15pc.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-=&-73lz.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-&@-80mw.html
http://www.lindahlbeck.com/22/Samantha/20141202232145-~_-19hd.html
http://www.lindahlbeck.com/22/Saman

タイトル
名前
URL
コメント