.NET クラスは、メソッドに同期論理をカプセル化します。
このレッスンでは、CustDB サンプル・データベースに関連するクラスをコンパイルします。
SQL Anywhere には、同期できるように設定された SQL Anywhere サンプル・データベース (CustDB) が付属しています。このデータベースには、同期に必要な SQL スクリプトなどが含まれています。たとえば、CustDB の ULCustomer テーブルは、さまざまなテーブル・レベル・イベントをサポートする同期テーブルです。
CustDB は、Ultra Light クライアントと SQL Anywhere クライアントの両方の統合データベース・サーバとなるように設計されています。CustDB データベースには、SQL Anywhere 11 CustDB という DSN が含まれています。
この項では、ULCustomer の upload_insert イベントと download_cursor イベントを処理するための論理を含む .NET クラス CustdbScripts を作成します。
.NET 同期論理を実行するには、Mobile Link サーバが iAnywhere.MobiLink.Script.dll 内のクラスにアクセスできることが必要です。iAnywhere.MobiLink.Script.dll には、.NET メソッドで利用する .NET クラス用 Mobile Link サーバ API のレポジトリが含まれています。
.NET 用 Mobile Link サーバ API の詳細については、.NET 用 Mobile Link サーバ API リファレンスを参照してください。
この API を利用する場合、CustdbScripts クラスのコンパイル時にここで示したアセンブリを含める必要があります。クラスのコンパイルは、Visual Studio を使用するか、コマンド・プロンプトから実行できます。
Visual Studio を使用する場合、新しいクラス・ライブラリを作成し、CustdbScripts コードを入力します。iAnywhere.MobiLink.Script.dll をリンクし、クラスのアセンブリを構築します。
テキスト・ファイルに CustdbScripts コードを入力し、ファイルを CustdbScripts.cs (Visual Basic .NET の場合は CustdbScripts.vb) として保存します。コマンド・ライン・コンパイラを使用して iAnywhere.MobiLink.Script.dll を参照し、クラスのアセンブリを構築します。
新しい Visual C# または Visual Basic .NET クラス・ライブラリ・プロジェクトを起動します。
プロジェクト名として CustdbScripts を使用し、適切なパスを入力します。このチュートリアルでは、パスを c:\mldnet とします。
CustdbScripts コードを入力します。
C# の場合、次のように入力します。
namespace MLExample { class CustdbScripts { public static string UploadInsert() { return("INSERT INTO ULCustomer(cust_id,cust_name) values (?,?)"); } public static string DownloadCursor(System.DateTime ts, string user ) { return("SELECT cust_id, cust_name FROM ULCustomer WHERE last_modified >= '" + ts.ToString("yyyy-MM-dd hh:mm:ss.fff") +"'"); } } } |
Visual Basic .NET の場合、次のように入力します。
Namespace MLExample Class CustdbScripts Public Shared Function UploadInsert() As String Return("INSERT INTO ULCustomer(cust_id,cust_name) values (?,?)") End Function Public Shared Function DownloadCursor(ByVal ts As System.DateTime, ByVal user As String) As String Return("SELECT cust_id, cust_name FROM ULCustomer " + _ "WHERE last_modified >= '" + ts.ToString("yyyy-MM-dd hh:mm:ss.fff") +"'") End Function End Class End Namespace |
Mobile Link サーバ API への参照を追加します。
Visual Studio の [プロジェクト] - [既存項目の追加] を選択します。
install-dir\Assembly\v2 にある iAnywhere.MobiLink.Script.dll を選択します。Visual Studio の場合は、[開く] - [リンク ファイル] を選択します。Visual Studio 2005 の場合は、[追加] - [リンクの追加] を選択します。
CustdbScripts プロジェクトを右クリックし、テーブルの [プロパティ] - [全般] を選択します。Visual Basic .NET の場合、すべてのテキストについて、[ルート名前空間] テキスト・フィールドがオフになっていることを確認してください。
CustdbScripts.dll をビルドします。
[ビルド] - [CustdbScripts のビルド] を選択します。
C:\mldnet\CustdbScripts\CustdbScripts\bin\Debug に CustdbScripts.dll が作成されます。
.NET クラスとアセンブリ用のディレクトリを作成します。
このチュートリアルでは、パスを c:\mldnet とします。
テキスト・エディタを使用して、CustdbScripts コードを入力します。
C# の場合、次のように入力します。
namespace MLExample { class CustdbScripts { public static string UploadInsert() { return("INSERT INTO ulcustomer(cust_id,cust_name) values (?,?)"); } public static string DownloadCursor(System.DateTime ts, string user ) { return("SELECT cust_id, cust_name FROM ULCustomer where last_modified >= '" + ts.ToString("yyyy-MM-dd hh:mm:ss.fff") +"'"); } } } |
Visual Basic .NET の場合、次のように入力します。
Namespace MLExample Class CustdbScripts Public Shared Function UploadInsert() As String Return("INSERT INTO ULCustomer(cust_id,cust_name) values (?,?)") End Function Public Shared Function DownloadCursor(ByVal ts As System.DateTime, ByVal user As String) As String Return("SELECT cust_id, cust_name FROM ULCustomer " + _ "WHERE last_modified >= '" + ts.ToString("yyyy-MM-dd hh:mm:ss.fff") +"'") End Function End Class End Namespace |
ファイルを CustdbScripts.cs (Visual Basic .NET の場合は CustdbScripts.vb) として c:\mldnet に保存します。
次のコマンドを使用して、ファイルをコンパイルします。
C# の場合、次のように入力します。
csc /out:c:\mldnet\custdbscripts.dll /target:library /reference:"%sqlany11%\Assembly\v2\iAnywhere.MobiLink.Script.dll" c:\mldnet\CustdbScripts.cs |
Visual Basic .NET の場合、次のように入力します。
vbc /out:c:\mldnet\custdbscripts.dll /target:library /reference:"%sqlany11%\Assembly\v2\iAnywhere.MobiLink.Script.dll" c:\mldnet\CustdbScripts.vb |
CustdbScripts.dll アセンブリが生成されます。
.NET 用 Mobile Link サーバ API の詳細については、.NET 用 Mobile Link サーバ API リファレンスを参照してください。
.NET メソッドの詳細については、メソッドを参照してください。
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |