ページの先頭行へ戻る
Interstage Shunsaku Data Manager V9.0.6 アプリケーション開発ガイド
FUJITSU Software

G.3.1 トランザクションとShunsaku Fileを使ったサンプル

トランザクション機能およびShunsaku Fileを使用する場合のC# .NETの使用例を示します。

ここでは、“5.1 トランザクションの概要”で説明した大学の講義の履修登録を例に、C# .NETの使用例を示します。


APIの使用例


以下にC# .NETを使用したプログラミング例を示します。

using System;
using Fujitsu.Shunsaku;

class SampleTransaction {
  static public void Main( string[] args ) {
    ShunService service = null;

    try {
      // ShunService の作成
      service = new ShunService();

      // ホスト名 ポート番号を指定して Shunsaku に接続
      service.Host = "DirSrv1";
      service.Port = 23101;
      service.ShunsakuFile = "shunsakuFile1";
      service.Connect();

      // 受講者数を増やしたいコースを検索するための検索条件を設定
      ShunSearchRequirement courseReq = new ShunSearchRequirement();
      courseReq.QueryExpression = "/course/name == 'Business negotiation'";
      courseReq.ReturnExpression = "/";

      // 更新対象のコース情報を検索
      ShunResultSet courseRs = service.Search( courseReq );

      // 検索結果からレコード情報を取得
      ShunRecord updateCourseRecord = 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>" );

      // 受講中の情報を追加したい生徒を検索するための検索式、リターン式を設定
      ShunSearchRequirement studentReq = new ShunSearchRequirement();
      studentReq.QueryExpression = "/student/e-mail == 'mary\\.tompson@example\\.com'";
      studentReq.ReturnExpression = "/";

      // 更新対象の生徒情報を検索
      ShunResultSet studentRs = service.Search( studentReq );

      // 検索結果からレコード情報を取得
      ShunRecord updateStudentRecord = 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();
        }
        throw;
      }
      
      // Shunsaku から切断
      service.Disconnect();

    }
    catch ( ShunContinuousException e ) {
      // ShunContinuousExceptionが発生した場合の処理を記述
      try {
        if( service != null && service.State == ShunConnectionState.Open ) {
          service.Disconnect();
        }
      }
      catch ( ShunException ex ) {
        Console.WriteLine("エラーメッセージ : {0}", ex.Message );
      }
      Console.WriteLine("エラーメッセージ : {0}", e.Message );
    }
    catch ( ShunConnectionTerminatedException e ) {
      // ShunConnectionTerminatedExceptionが発生した場合の処理を記述
      Console.WriteLine("エラーメッセージ : {0}", e.Message );
    }
  }
}

実行結果


コース情報の更新終了
学生情報の更新終了