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

SQL Anywhere 11.0.1 (中文) » UltraLite - C 及 C++ 编程 » 教程:使用 C++ API 构建应用程序

 

第 4 课:将同步添加到应用程序

本课介绍如何同步应用程序和计算机上运行的统一数据库。

以下过程将同步代码添加到应用程序,启动 MobiLink 服务器,然后运行应用程序进行同步。

在前几节课中创建的 UltraLite 数据库与 UltraLite 11 示例数据库同步。UltraLite 11 示例数据库中 ULCustomer 表的列包含本地 UltraLite 数据库中 customer 表的列。

本课假设您熟悉 MobiLink 同步。

♦  将同步添加到应用程序
  1. 将以下方法添加到 customer.cpp。此方法执行以下任务:

    • 通过调用 ULEnableTcpipSynchronization 将同步流设置为 TCP/IP。同步还可以通过 HTTP、HotSync 或 HTTPS 执行。请参见UltraLite 客户端

    • 设置脚本版本。MobiLink 同步由存储在统一数据库中的脚本控制。脚本版本确定要使用的脚本集。

    • 设置 MobiLink 用户名。此值用于 MobiLink 服务器处的验证。它与 UltraLite 数据库用户 ID 不同,尽管在某些应用程序中您可能希望赋予它们相同的值。

    • 将 download_only 参数设置为 true。缺省情况下,MobiLink 同步是双向的。此应用程序使用仅下载同步,因此表中的行不会上载到示例数据库中。

    bool do_sync( Connection * conn ) {
      ul_synch_info info;
      ul_stream_error * se = &info.stream_error;
      
      ULEnableTcpipSynchronization( Tutca.GetCA() );
      conn->InitSynchInfo( &info );
      info.stream = ULSocketStream();
      info.version = UL_TEXT( "custdb 11.0" );
      info.user_name = UL_TEXT( "50" );
      info.download_only = true;
      if( !conn->Synchronize( &info ) ) {
          _tprintf( _TEXT("Synchronization error \n" ));   
       _tprintf( _TEXT("  stream_error_code is '%lu'\n"), se->stream_error_code );
       _tprintf( _TEXT("  system_error_code is '%ld'\n"), se->system_error_code );
       _tprintf( _TEXT("  error_string is '") );
       _tprintf( _TEXT("%s"), se->error_string );
       _tprintf( _TEXT("'\n") );
       return false;
      }
      return true;
    }
  2. 将下行添加到 main 方法中,放在调用 insert 方法的语句和调用 select 方法的语句之间:

    do_sync(conn);
  3. 通过运行 nmake 编译您的应用程序。

♦  同步数据
  1. 启动 MobiLink 服务器。

    在命令提示符下,运行以下命令:

    mlsrv11 -c "dsn=SQL Anywhere 11 CustDB" -v+ -zu+

    -zu+ 选项提供自动添加用户的功能。-v+ 选项为所有消息启用详细记录。

    有关此选项的详细信息,请参见MobiLink 服务器选项

  2. 通过在命令提示符下键入 customer 运行应用程序。

    MobiLink 服务器窗口显示指示同步进度的状态消息。如果同步成功,则最后一条消息显示 [Synchronization complete]。