Google
 
Web itpro-blogger.blogspot.com
金曜日, 11月 23, 2007

[VB2005]複数テーブルのデータベース更新

▼質問
複数テーブルを更新する時にトランザクションの管理やエラーの時のロールバックや成功の時の
コミットはどうやるんだい?

▼回答
サンプルを示しておくから参考にしてくれ!

'接続内容設定
Dim strCon as String = "Data Source=サービス・ネーミング" & _
";User ID=ユーザ名" & _
";Password=パスワード"

'データベースコネクション確立
Dim con As OracleConnection = New OracleConnection(strConnection)

'データベースオープン
con.Open()

Dim txn As OracleTransaction = con.BeginTransaction(IsolationLevel.Serializable)

Try

'レコード削除
Call DeleteTable(con)
'削除関連情報更新
Call UpdateTable(con)

txn.Commit()

Catch ex As Exception

txn.Rollback()

Finally

'データベースクローズ
con.Close()
con.Dispose()
con = Nothing

End Try

Private Sub DeleteTable(ByVal con As OracleConnection)

Dim cmd As OracleCommand = Nothing

Dim strSql As String = "DELETE FROM テーブル名 " & _
"WHERE フィールド名 = 1"

Try

cmd = New OracleCommand(strSql, con)
cmd.ExecuteNonQuery()

Catch ex As Exception
Throw
Finally

If (Not IsNothing(cmd)) Then
cmd.Dispose()
cmd = Nothing
End If

End Try

End Sub

Private Sub UpdateTable(ByVal con As OracleConnection)

Dim cmd As OracleCommand = Nothing

Dim strSql As String = "UPDATE テーブル名 " & _
"SET フィールド名 = 0 " & _
"WHERE フィールド名 = 1"

Try

cmd = New OracleCommand(strSql, con)
cmd.ExecuteNonQuery()

Catch ex As Exception
Throw
Finally

If (Not IsNothing(cmd)) Then
cmd.Dispose()
cmd = Nothing
End If

End Try

End Sub


■参考サイト
Oracle Data Provider for .NETのクラス
http://otndnld.oracle.co.jp/document/products/oracle10g/101/doc_v12/win.101/B15519-01/OracleTransactionClass.htm






Comments: コメントを投稿

<< Home

This page is powered by Blogger. Isn't yours?