HIRO's.NET Blog

PowerShellが好き

目次

Blog 利用状況

ニュース

あわせて読みたい

書庫

日記カテゴリ

リンク

VB.NETからOracleストアドプロシージャを呼び出す その2

前回は引数なしのストアドプロシージャをVB.NETから呼び出す方法を紹介しました。

今回は、引数があるストアドプロシージャをVB.NETから呼び出す方法を紹介します。

 

開発言語:VB.NET 2005

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

ストアドプロシージャは下記の通りです。もらった引数をINSERT文で書き込んでいるだけのものです。

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

■ストアドプロシージャ2
CREATE OR REPLACE PROCEDURE SCOTT.PROCEDURE2(in_EMPNO in varchar2, in_ENAME in varchar2, in_SAL in number)
IS
BEGIN
    INSERT INTO EMP (EMPNO, ENAME, SAL) VALUES (in_EMPNO, in_ENAME, in_SAL);
END;
/

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

使用方法は、見ていただければわかる通りほぼ一緒です。

違う箇所と言えば、ストアドプロシージャの引数に渡す値の設定の仕方です。

 

OracleのVarchar2に対して

ODP.NETではOracleDbType.Varchar2を

OracleClientではOracleType.VarCharを

OleDbではOleDbType.VarCharを指定します。
 

OracleのNumberに対しては

ODP.NETではOracleDbType.Decimal

OracleClientではOracleType.Number

OleDbではOleDbType.Decimalを指定します。

 

それとCmd.Parameters.Addの第1引数ですが、ストアドプロシージャの引数名と合わせておいた方が良いようです。

(OracleClientでは合わせておかないとうまく呼び出すことができませんでした)

■ODP.NETサンプル
Private Sub ODP_TEST2()
    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 = "PROCEDURE2"
                'ストアドプロシージャの引数に渡す値の設定
                Cmd.Parameters.Add("in_EMPNO", OracleDbType.Varchar2).Value = "7777"
                Cmd.Parameters.Add("in_ENAME", OracleDbType.Varchar2).Value = "HIRO"
                Cmd.Parameters.Add("in_SAL", OracleDbType.Decimal).Value = 500
                'ストアドプロシージャの実行
                Cmd.ExecuteNonQuery()

                MessageBox.Show("PROCEDURE2 正常終了", "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_TEST2()
    Dim builder As New System.Data.OracleClient.OracleConnectionStringBuilder
    ''ORACLE接続文字列
    With builder
        .DataSource = "YourServer"
        .UserID = "scott"
        .Password = "tiger"
    End With

    Try
        'Using OraConn As New OracleConnection("user id=scott;password=tiger;data source=YourServer")
        Using OraConn As New OracleConnection(builder.ConnectionString)
            Using Cmd As New OracleCommand
                'データベース接続を開く
                OraConn.Open()
                Cmd.Connection = OraConn
                'コマンドタイプをストアドプロシージャにする
                Cmd.CommandType = CommandType.StoredProcedure
                '実行するストアドプロシージャを指定
                Cmd.CommandText = "PROCEDURE2"
                'ストアドプロシージャの引数に渡す値の設定
                'パラメータの名前(in_EMPNOなどはストアドプロシージャと一致させる必要がある)
                Cmd.Parameters.Add("in_EMPNO", OracleType.VarChar).Value = "7777"
                Cmd.Parameters.Add("in_ENAME", OracleType.VarChar).Value = "HIRO"
                Cmd.Parameters.Add("in_SAL", OracleType.Number).Value = 500
                'ストアドプロシージャの実行
                Cmd.ExecuteNonQuery()

                MessageBox.Show("PROCEDURE2 正常終了", "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_TEST2()
    Try
        Using OraConn As New OleDbConnection("Provider=MSDAORA.1.Oracle;user id=scott;password=tiger;data source=YourServer")
            Using Cmd As New OleDbCommand
                'データベース接続を開く
                OraConn.Open()
                Cmd.Connection = OraConn
                'コマンドタイプをストアドプロシージャにする
                Cmd.CommandType = CommandType.StoredProcedure
                '実行するストアドプロシージャを指定
                Cmd.CommandText = "PROCEDURE2"
                'ストアドプロシージャの引数に渡す値の設定
                Cmd.Parameters.Add("in_EMPNO", OleDbType.VarChar).Value = "7777"
                Cmd.Parameters.Add("in_ENAME", OleDbType.VarChar).Value = "HIRO"
                Cmd.Parameters.Add("in_SAL", OleDbType.Decimal).Value = 500
                'ストアドプロシージャの実行
                Cmd.ExecuteNonQuery()

                MessageBox.Show("PROCEDURE2 正常終了", "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月18日 21:58

コメントを追加

# VB.NETからOracleストアドプロシージャを呼び出す その3(VB.NET Tips) 2008/03/21 23:47 HIRO's.NET Blog

前回(その2) は引数あり(IN引数のみ)のストアドプロシージャをVB.NETから呼び出す方法を紹介しました。 今回は、OUT引数があるストアドプロシージャをVB.NETから呼び出す方法を紹介します。

# VB.NETからOracleストアドプロシージャを呼び出す その4(VB.NET Tips) 2008/03/21 23:50 HIRO's.NET Blog

この記事は2007/04/20にわんくまBlogへ書いたものです。 前回(その3) は引数ありのストアドプロシージャ のOUTパラメータを受け取る方法を紹介しました。 今回は、REF CURSORをOUT引数として持つストアドプロシージャをVB

# Xiwby Szk Yqiu Vjs Dbsews 2014/11/30 21:40 Robertwat

www.delta-bearings.com/imgs/201411269372727113.html
www.innomont.com/imgs/201411269415126775.html
mike.maliska.net/imgs/201411269351760800.html
www.grpone.com/imgs/201411269581947315.html
www.crompton-canadaeast.com/imgs/201411269522263271.html
http://www.navarrebeachregency21.com/imgs/201411269304222168.html
wisconsinturfgrassassociation.org/imgs/201411269465864658.html
www.anaar.com/imgs/201411269462836069.html
www.bollmanfirm.com/imgs/201411269534262422.html
http://www.kavanaspa.com/imgs/20141126928786634.html
www.mgmlimo.com/imgs/20141126945211402.html
www.montridgellc.com/imgs/20141126957061423.html
www.leatherming.com/imgs/201411269564556959.html
kcdiamondsports.com/imgs/201411269483095342.html
wisconsinturfgrassassociation.org/imgs/20141126947184885.html

# Iylbn Hks Xvdt Wqg Nvdith 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
コメント