Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » MobiLink - 入门 » MobiLink 教程 » 教程:使用 .NET 同步逻辑

 

第 1 课:用 MobiLink 参考编译 CustdbScripts.dll 程序集

.NET 类将同步逻辑封装在方法中。

在本课中,您将编译一个与 CustDB 示例数据库关联的类。

MobiLink 数据库示例

SQL Anywhere 随附一个 SQL Anywhere 示例数据库 (CustDB),该数据库已设置为可以进行同步,其中包括驱动同步所需的 SQL 脚本。例如,CustDB ULCustomer 表是一个支持多种表级别事件的同步表。

CustDB 的设计用途是作为 UltraLite 和 SQL Anywhere 客户端的统一数据库服务器。CustDB 数据库有一个名为 SQL Anywhere 11 CustDB 的 DSN。

CustdbScripts 程序集

在本节中,您将创建一个名为 CustdbScripts 的 .NET 类,其中包含用于处理 ULCustomer upload_insert 和 download_cursor 事件的逻辑。

MobiLink 服务器 API

要执行 .NET 同步逻辑,MobiLink 服务器必须能够访问 iAnywhere.MobiLink.Script.dll 中的类。iAnywhere.MobiLink.Script.dll 包含要在 .NET 方法中使用的面向 .NET 的 MobiLink 服务器 API 类的存储库。

有关用于 .NET 的 MobiLink 服务器 API 的详细信息,请参见用于 .NET 参考的 MobiLink 服务器 API

编译 CustdbScripts 类时,必须包含此程序集才能使用该 API。可以使用 Visual Studio 或在命令提示符处编译类。

  • 在 Visual Studio 中,创建一个新的类库,并输入 CustdbScripts 代码。链接 iAnywhere.MobiLink.Script.dll,并构建类的程序集。

  • 将 CustdbScripts 代码写入文本文件,并将该文件保存为 CustdbScripts.cs(对于 Visual Basic .NET,保存为 CustdbScripts.vb)。使用命令行编译器,引用 iAnywhere.MobiLink.Script.dll 并构建类的程序集。

♦  使用 Visual Studio 创建 CustdbScripts 程序集
  1. 启动一个新的 Visual C# 或 Visual Basic .NET 类库项目。

    使用 CustdbScripts 作为项目名,并输入相应的路径。本教程假定路径为 c:\mldnet

  2. 输入 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
  3. 添加对 MobiLink 服务器 API 的引用。

    • 从 Visual Studio 的 [Project] 菜单中,选择 [Add Existing Item]。

    • install-dir\Assembly\v2 中选择 iAnywhere.MobiLink.Script.dll。在 Visual Studio 的 [Open] 菜单中,选择 [Link File]。在 Visual Studio 2005 的 [Add] 菜单中选择 [Add Link]。

  4. 右击 CustdbScripts 项目并选择表 [Properties] » [General]。对于 Visual Basic .NET,请确保 [Root Namespace] 文本字段中没有任何文本。

  5. 构建 CustdbScripts.dll

    在 [Build] 菜单中,选择 [Build CustdbScripts]。

    将在 C:\mldnet\CustdbScripts\CustdbScripts\bin\Debug 目录中创建 CustdbScripts.dll

♦  在命令提示符中创建 CustdbScripts 程序集
  1. 为 .NET 类和程序集创建一个目录。

    本教程假定路径为 c:\mldnet

  2. 使用文本编辑器输入 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
  3. c:\mldnet 中,将文件保存为 CustdbScripts.cs(对于 Visual Basic .NET,保存为 CustdbScripts.vb)。

  4. 使用以下命令编译该文件。

    如果是 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 的 MobiLink 服务器 API 的详细信息,请参见用于 .NET 参考的 MobiLink 服务器 API

有关 .NET 方法的详细信息,请参见方法