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

G.2.3 データを更新する

データを更新する場合の、C# .NETの使用例を示します。


検索条件

「2006年7月18日に神奈川で宿泊可能なホテルのうち、ホテル1の情報を更新したい。」

年月日(2006年7月18日)を条件に検索を行い、ホテル名『ホテル1』と一致したデータを更新します。


APIの使用例


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

using System;
using Fujitsu.Shunsaku;

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

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

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

      // ShunSearchRequirement の作成
      ShunSearchRequirement req = new ShunSearchRequirement();

      // 各種検索条件の設定
      req.QueryExpression = "/document/base/name == 'ホテル1'";
      req.ReturnExpression = "/document/base/name/text()";
      req.ReplyNumber = 1;
      req.RequestCount = 30;

      // 検索条件を指定して検索を実行
      ShunResultSet rs = service.Search( req );

      // 更新用 ShunRecordCollection を作成
      ShunRecordCollection updateRecords = new ShunRecordCollection();

      // 検索条件に該当するレコードを取得
      foreach ( ShunRecord record in rs.Records ) {
        if ( record.Data == "ホテル1" ) {
          // ホテル1の情報について、レコードID、更新データを設定
          record.Data = String.Concat(
            "<document>",
            "    <base>",
            "        <name>ホテル1</name>",
            "        <prefecture>大阪</prefecture>",
            "        <address>大阪府大阪市中央区</address>",
            "        <detail>http://xxxxx.co.jp</detail>",
            "        <price>8000</price>",
            "    </base>",
            "    <information>",
            "        <date>2006年07月18日</date>",
            "    </information>",
            "    <note>バス付 トイレ付 地下鉄 △△駅徒歩02分</note>",
            "</document>" );
          updateRecords.Add( record );
        }
      }

      // データ設定に成功している場合、データを更新
      if ( updateRecords.Count > 0 ) {
        service.Update( updateRecords );
        Console.WriteLine( "更新終了" );
      }

      // 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 );
    }
  }
}

実行結果


更新終了