ODP.NET トランザクション例

OracleTransactionオブジェクトを作成し、OracleConnectionオブジェクトのBeginTransaction メソッドを呼び出してトランザクションを開始します。

サンプル


EMP表を更新します。正常に更新が終了した場合、トランザクションをコミットします。
ENAMEはサイズ10で、10文字以上入れるとエラーとなり、トランザクションをロールバックします。

(エラー時)
エラー時の画面

Imports Oracle.DataAccess.Client

Public Class frmTransaction

    Private Const ConnectionString As String = "DATA SOURCE=ORCL;User ID=scott;password=tiger;"

    Private dt As DataTable = Nothing

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Const cmdSelect As String = "select EMPNO,ENAME from EMP order by EMPNO"

        Using da As New OracleDataAdapter(cmdSelect, ConnectionString)
            dt = New DataTable
            da.Fill(dt)
            Me.C1TrueDBGrid1.DataSource = dt
        End Using

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim No1_No As Integer = CInt(TextEMPNO1.Text)
        Dim No1_Name As String = TextENAME1.Text
        Dim No2_No As Integer = CInt(TextEMPNO2.Text)
        Dim No2_Name As String = TextENAME2.Text

        UpdateData(No1_No, No1_Name, No2_No, No2_Name)


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    ''' 
    ''' EMP表の更新
    ''' 
    ''' 更新1 EMPNO
    ''' 更新1 ENAME
    ''' 更新2 EMPNO
    ''' 更新2 ENAME
    ''' 
    Private Sub UpdateData(ByVal No1_No As Integer, ByVal No1_Name As String, ByVal No2_No As Integer, ByVal No2_Name As String)
        Const cmdUpdate As String = "update emp set ename=:ENAME where empno=:EMPNO"

        Dim cn As OracleConnection = Nothing
        Dim trn As OracleTransaction = Nothing

        Try
            cn = New OracleConnection(ConnectionString)
            '接続を開く
            cn.Open()

            'トランザクション開始
            trn = cn.BeginTransaction

            Using cmd As New OracleCommand(cmdUpdate, cn)
                '更新1
                cmd.BindByName = True
                cmd.Parameters.Add("ENAME", OracleDbType.Varchar2).Value = No1_Name
                cmd.Parameters.Add("EMPNO", OracleDbType.Int32).Value = No1_No
                cmd.ExecuteNonQuery()

                '更新2
                cmd.Parameters.Item("ENAME").Value = No2_Name
                cmd.Parameters.Item("EMPNO").Value = No2_No
                cmd.ExecuteNonQuery()

            End Using

            'コミット
            trn.Commit()

            MessageBox.Show("更新完了", "正常終了", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception
            If Not IsNothing(trn) Then
                trn.Rollback()
            End If
            MessageBox.Show(ex.Message, "更新前の状態に戻しました", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally
            If Not IsNothing(trn) Then
                trn.Dispose()
            End If

            If Not IsNothing(cn) Then
                If Not (cn.State = ConnectionState.Closed) Then
                    cn.Close()
                End If
                cn.Dispose()
            End If
        End Try

    End Sub
End Class

履歴

2011/09/05
公開
Loading