在本课中,您将编译一个与 CustDB 示例数据库关联的类。
SQL Anywhere 随附一个 SQL Anywhere 示例数据库 (CustDB),该数据库已进行了同步设置,其中包括同步所需的 SQL 脚本。例如,CustDB ULCustomer 表是一个同步表,它支持多种表级别事件。
CustDB 的设计用途是作为 UltraLite 和 SQL Anywhere 客户端的统一数据库服务器。CustDB 数据库具有名为 SQL Anywhere 12 CustDB 的 ODBC 数据源。
.NET 类将同步逻辑封装在方法中。在本节中,您将创建一个名为 CustdbScripts 的 .NET 类,其中包含用于处理 ULCustomer upload_insert 和 download_cursor 事件的逻辑。
要执行 .NET 同步逻辑,MobiLink 服务器必须能够访问 iAnywhere.MobiLink.Script.dll 中的类。iAnywhere.MobiLink.Script.dll 包含要在 .NET 方法中使用的面向 .NET 的 MobiLink 服务器 API 类的存储库。
有关用于 .NET 的 MobiLink 服务器 API 的详细信息,请参见MobiLink 服务器 .NET API 参考。
编译 CustdbScripts 类时,必须包含此程序集才能使用该 API。可以使用 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 程序集,必须在装载 MobiLink 服务器时显式包含 -clrVersion 选项。 有关 -clrVersion 选项的详细信息,请参见-sl dnet mlsrv12 选项。
确保 Class1.cs 文件在 Solution Explorer 中突出显示。
输入 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 项目,然后选择表 [Properties] » [General]。请确保 [Root Namespace] 文本字段中没有任何文本。
构建 CustdbScripts.dll。
单击 [Build] » [Build CustdbScripts]。
将在C:\mldotnet\CustdbScripts\CustdbScripts\bin\Debug 中创建 CustdbScripts.dll 文件。
可以使用命令行、文本编辑器和 Visual Studio 编译器代替 Visual Studio IDE 来创建 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 |
在 c:\mldotnet 中,将文件另存为 CustdbScripts.cs(对于 Visual Basic,则另存为 CustdbScripts.vb)。
编译文件。
对于 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讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |