このレッスンでは、CustDB サンプルデータベースに関連するクラスをコンパイルします。
SQL Anywhere には、同期できるように設定された SQL Anywhere サンプルデータベース (CustDB) が付属しています。このデータベースには、同期に必要な SQL スクリプトなどが含まれています。たとえば、CustDB の ULCustomer テーブルは、さまざまなテーブルレベルイベントをサポートする同期テーブルです。
CustDB は、Ultra Light クライアントと SQL Anywhere クライアントの両方の統合データベースサーバーとなるように設計されています。CustDB 統合データベースは、ODBC データソース SQL Anywhere 12 CustDB を使用します。
.NET クラスは、メソッドに同期論理をカプセル化します。この項では、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 の詳細については、Mobile Link サーバー .NET API リファレンスを参照してください。
この API を利用する場合、CustdbScripts クラスのコンパイル時にここで示したアセンブリを含める必要があります。クラスのコンパイルは、Visual Studio を使用するか、コマンドプロンプトから実行できます。
新しいクラスライブラリを作成し、CustdbScripts コードを入力します。iAnywhere.MobiLink.Script.dll をリンクし、クラスのアセンブリを構築します。
テキストファイルに CustdbScripts コードを入力し、ファイルを CustdbScripts.cs (Visual Basic の場合は CustdbScripts.vb) として保存します。コマンドラインコンパイラーを使用して iAnywhere.MobiLink.Script.dll を参照し、クラスのアセンブリを構築します。
Visual Studio を使用すると、CustdbScripts アセンブリを作成できます。
新しい Visual C# または Visual Basic Windows クラスライブラリプロジェクトを起動します。
プロジェクトの [名前] として CustdbScripts を使用し、適切なパスを入力します。このチュートリアルでは、パスを c:\mldotnet とします。
Visual Studio 2010 では、トップのドロップボックスを [.NET Framework 4] から [.NET Framework 3.5] に変更します。
v4.0 のアセンブリを使用するには、Mobile Link サーバーをロードするときに -clrVersion オプションを明示的に含める必要があります。 -clrVersion オプションの詳細については、-sl dnet mlsrv12 オプションを参照してください。
ソリューションエクスプローラーで、Class1.cs ファイルが強調表示されていることを確認します。
CustdbScripts コードを入力します。
C# の場合は、既存のコードを次のコードで置き換えます。
namespace MLExample { class CustdbScripts { public static string UploadInsert() { return("INSERT INTO ULCustomer(cust_id,cust_name) values ({ml r.cust_id}, {ml r.cust_name})"); } 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 の場合は、既存のコードを次のコードで置き換えます。
Namespace MLExample Class CustdbScripts Public Shared Function UploadInsert() As String Return("INSERT INTO ULCustomer(cust_id,cust_name) values ({ml r.cust_id}, {ml r.cust_name})") 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 |
Visual Basic の場合は、CustdbScripts プロジェクトを右クリックし、テーブルの [プロパティ] » [一般] を選択します。すべてのテキストについて、[ルート名前空間] テキストフィールドがオフになっていることを確認してください。
CustdbScripts.dll をビルドします。
[構築] » [CustdbScripts の構築] をクリックします。
C:\mldotnet\CustdbScripts\CustdbScripts\bin\Debug にこのファイル CustdbScripts.dll が作成されます。
Visual Studio IDE の代わりにコマンドライン、テキストエディター、Visual Studio コンパイラーを使用して、CustdbScripts アセンブリを作成できます。
.NET クラスとアセンブリ用のディレクトリを作成します。
このチュートリアルでは、パスを c:\mldotnet とします。
テキストエディターを使用して、CustdbScripts コードを入力します。
C# の場合、次のように入力します。
namespace MLExample { class CustdbScripts { public static string UploadInsert() { return("INSERT INTO ULCustomer(cust_id,cust_name) values ({ml r.cust_id}, {ml r.cust_name})"); } 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 の場合は、次のように入力します。
Namespace MLExample Class CustdbScripts Public Shared Function UploadInsert() As String Return("INSERT INTO ULCustomer(cust_id,cust_name) values ({ml r.cust_id}, {ml r.cust_name})") 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 の場合は CustdbScripts.vb) として c:\mldotnet に保存します。
ファイルをコンパイルします。
C# の場合は、次のコマンドを実行します。
csc /out:c:\mldotnet\custdbscripts.dll /target:library /reference:"C:\Program Files\SQL Anywhere 12\Assembly\v2\iAnywhere.MobiLink.Script.dlmldotnetmldotnet\CustdbScripts.cs |
Visual Basic の場合は、次のコマンドを実行します。
vbc /out:c:\mldotnet\custdbscripts.dll /target:library /reference:"C:\Program Files\SQL Anywhere 12\Assembly\v2\iAnywhere.MobiLink.Script.dll" c:\mldotnet\CustdbScripts.vb |
CustdbScripts.dll アセンブリが生成されます。
![]() |
DocCommentXchange で意見交換できます
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |