トランザクション機能およびShunsaku Fileを使用する場合のVB .NETの使用例を示します。
ここでは、“5.1 トランザクションの概要”で説明した大学の講義の履修登録を例に、VB .NETの使用例を示します。
以下にVB .NETを使用したプログラミング例を示します。
Imports System Imports Fujitsu.Shunsaku Imports System.ComponentModel Class SampleDelete Shared Public Function Main( ByVal args() As String ) As Integer Dim service As ShunService = Nothing Try '' ShunService の作成 service = new ShunService() '' ホスト名 ポート番号を指定して Shunsaku に接続 service.Host = "DirSrv1" service.Port = 23101 service.ShunsakuFile = "shunsakuFile1" service.Connect() '' 受講者数を増やしたいコースを検索するための検索条件を設定 Dim courseReq As ShunSearchRequirement = new ShunSearchRequirement() courseReq.QueryExpression = "/course/name == 'Business negotiation'" courseReq.ReturnExpression = "/" '' 更新対象のコース情報を検索 Dim courseRs As ShunResultSet = service.Search( courseReq ) '' 検索結果からレコード情報を取得 Dim updateCourseRecord As ShunRecord = courseRs.Records( 0 ) '' コース情報更新データ updateCourseRecord.Data = String.Concat( _ "<course>", _ " <name>Business negotiation</name>", _ " <instructor>", _ " <first-name>Max</first-name>", _ " <last-name>cameron</last-name>", _ " </instructor>", _ " <capacity>40</capacity>", _ " <current-auditors>31</current-auditors>", _ "</course>" ) '' 受講中の情報を追加したい生徒を検索するための検索式、リターン式を設定 Dim studentReq As ShunSearchRequirement = new ShunSearchRequirement() studentReq.QueryExpression = "/student/e-mail == 'mary\.tompson@example\.com'" studentReq.ReturnExpression = "/" '' 更新対象の生徒情報を検索 Dim studentRs As ShunResultSet = service.Search( studentReq ) '' 検索結果からレコード情報を取得 Dim updateStudentRecord As ShunRecord = studentRs.Records( 0 ) '' 生徒情報更新データ updateStudentRecord.Data = String.Concat( _ "<student>", _ " <first-name>Mary</first-name>", _ " <last-name>Tompson</last-name>", _ " <e-mail>mary.tompson@example.com</e-mail>", _ " <course>Chinese language</course>", _ " <course>Business negotiation</course>", _ "</student>" ) '' 自動コミットを無効に設定 service.AutoCommit = False Try '' コース情報の更新 service.Update( updateCourseRecord ) Console.WriteLine( "コース情報の更新終了" ) '' 生徒情報の更新 service.Update( updateStudentRecord ) Console.WriteLine( "生徒情報の更新終了" ) '' ここまでの更新をコミット service.Commit() Catch If service.State = ShunConnectionState.Open service.Rollback() End If Throw End Try '' Shunsaku から切断 service.Disconnect() Catch e As ShunContinuousException '' ShunContinuousExceptionが発生した場合の処理を記述 Try If Not service Is Nothing And service.State = ShunConnectionState.Open Then service.Disconnect() End If Catch ex As ShunException Console.WriteLine( "エラーメッセージ : {0}", ex.Message ) End Try Console.WriteLine( "エラーメッセージ : {0}", e.Message ) Catch e As ShunConnectionTerminatedException '' ShunConnectionTerminatedExceptionが発生した場合の処理を記述 Console.WriteLine( "エラーメッセージ : {0}", e.Message ) End Try End Function End Class |
実行結果
コース情報の更新終了 生徒情報の更新終了 |