.NET classes encapsulate synchronization logic in methods.
In this lesson, you compile a class associated with the CustDB sample database.
SQL Anywhere ships with a SQL Anywhere sample database (CustDB) that is already set up for synchronization, including the SQL scripts required for synchronization. The CustDB ULCustomer table, for example, is a synchronized table that supports a variety of table-level events.
CustDB is designed to be a consolidated database server for both UltraLite and SQL Anywhere clients. The CustDB database has a DSN named SQL Anywhere 12 CustDB.
In this section, you create a .NET class called CustdbScripts with logic to handle the ULCustomer upload_insert and download_cursor events.
To execute .NET synchronization logic, the MobiLink server must have access to the classes in iAnywhere.MobiLink.Script.dll. iAnywhere.MobiLink.Script.dll contains a repository of MobiLink server API for .NET classes to use in your .NET methods.
For more information about the MobiLink server API for .NET, see MobiLink server .NET API reference (.NET 2.0).
When compiling the CustdbScripts class, you must include this assembly to make use of the API. You can compile your class using Visual Studio or at a command prompt.
In Visual Studio, create a new Class Library and enter the CustdbScripts code. Link iAnywhere.MobiLink.Script.dll, and build the assembly for your class.
Put the CustdbScripts code in a text file and save the file as CustdbScripts.cs ( or CustdbScripts.vb for Visual Basic). Using a command line compiler, reference iAnywhere.MobiLink.Script.dll and build the assembly for your class.
Start a new Visual C# or Visual Basic Windows Class Library project.
Use CustdbScripts for the project Name and enter an appropriate path. This tutorial assumes the path c:\mldnet.
Ensure that the Class1.cs file is highlighted in the Solution Explorer.
Enter the CustdbScripts code.
The sample code in this tutorial illustrates a deprecated coding practice. It is recommended that you do not return SQL code when writing Java or .NET scripts. You should use make changes to the consolidated database directly from Java or .NET.
For more information, see Direct row handling.
For C#, replace the existing code with the following:
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") +"'"); } } } |
For Visual Basic, replace the existing code with the following:
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 |
Add a reference to the MobiLink server API.
From the Visual Studio Project menu, choose Add Existing Item.
Select iAnywhere.MobiLink.Script.dll in C:\Program Files\SQL Anywhere 12\Assembly\V2. Replace C:\Program Files\SQL Anywhere 12 with the directory of your SQL Anywhere installation.
From the Add dropdown, choose to add the file as a link.
For Visual Basic, right-click the CustdbScripts project and select the table Properties » General. Make sure that the text field Root Namespace is cleared of all text.
Build CustdbScripts.dll.
From the Build menu, choose Build CustdbScripts.
This creates CustdbScripts.dll in C:\mldnet\CustdbScripts\CustdbScripts\bin\Debug.
Create a directory for the .NET class and assembly.
This tutorial assumes the path c:\mldnet.
Using a text editor, enter the CustdbScripts code.
The sample code in this tutorial illustrates a deprecated coding practice. It is recommended that you do not return SQL code when writing Java or .NET scripts. You should use make changes to the consolidated database directly from Java or .NET.
For more information, see Direct row handling.
For C#, type:
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") +"'"); } } } |
For Visual Basic, type:
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 |
Save the file as CustdbScripts.cs ( CustdbScripts.vb for Visual Basic ) in c:\mldnet.
Compile the file.
For C#, run the following command:
csc /out:c:\mldnet\custdbscripts.dll /target:library /reference:"C:\Program Files\SQL Anywhere 12\Assembly\v2\iAnywhere.MobiLink.Script.dll" c:\mldnet\CustdbScripts.cs |
For Visual Basic, run the following command:
vbc /out:c:\mldnet\custdbscripts.dll /target:library /reference:"C:\Program Files\SQL Anywhere 12\Assembly\v2\iAnywhere.MobiLink.Script.dll" c:\mldnet\CustdbScripts.vb |
The CustdbScripts.dll assembly is generated.
For more information about the MobiLink server API for .NET, see MobiLink server .NET API reference (.NET 2.0).
For more information about .NET methods, see Methods.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |