如何同步订阅(以编程方式)

本主题将介绍如何使用 SqlCeReplication 类同步 Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) 中的订阅。有关使用 SqlServerCe 命名空间的详细信息,请参阅 SqlServerCe 命名空间参考文档。

过程标题

  1. 初始化 SqlCeReplication 对象。

    SqlCeReplication repl = new SqlCeReplication();
    
  2. 设置连接属性。这些属性指定所订阅发布的名称和位置、本地 SQL Server Compact Edition 数据库的名称和位置以及 SQL Server Compact Edition 服务器代理的位置。

    repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
    repl.InternetLogin = "MyInternetLogin";
    repl.InternetPassword = "<password>";
    repl.Publisher = "MyPublisher";
    repl.PublisherDatabase = "MyPublisherDatabase";
    repl.PublisherLogin = "MyPublisherLogin";
    repl.PublisherPassword = "<password>";
    repl.Publication = "MyPublication";
    repl.Subscriber = "MySubscriber";
    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
    
  3. 调用 AddSubscription 方法创建订阅。

    repl.AddSubscription(AddOption.CreateDatabase);
    
  4. 调用 Synchronize 方法执行数据同步,并用发布中的数据填充本地数据库。

    repl.Synchronize();
    

示例

本示例说明如何创建订阅并与 Microsoft SQL Server 同步数据。在此示例中,本地数据库的名称为 MyDatabase.sdf,发布的名称为 MyPublication。

SqlCeReplication repl = null;

        try
        {
            // Create an instance of and configure the SqlCeReplication object
            //
            repl = new SqlCeReplication();
            repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
            repl.InternetLogin = "MyInternetLogin";
            repl.InternetPassword = "<password>";
            repl.Publisher = "MyPublisher";
            repl.PublisherDatabase = "MyPublisherDatabase";
            repl.PublisherLogin = "MyPublisherLogin";
            repl.PublisherPassword = "<password>";
            repl.Publication = "MyPublication";
            repl.Subscriber = "MySubscriber";
            repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";

            // Create a local SQL Server Compact Edition Database subscription
            //
            repl.AddSubscription(AddOption.CreateDatabase);

            // Synchronize to the SQL Server database
            //
            repl.Synchronize();
        }
        catch (SqlCeException)
        {
            // Handle errors here
            //
        }
        finally
        {
            // Dispose the repl object
            //
            repl.Dispose();
        }
Dim repl As SqlCeReplication = Nothing

        Try
            ' Create an instance of and configure the SqlCeReplication object
            '
            repl = New SqlCeReplication()
            repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll"
            repl.InternetLogin = "MyInternetLogin"
            repl.InternetPassword = "<password>"
            repl.Publisher = "MyPublisher"
            repl.PublisherDatabase = "MyPublisherDatabase"
            repl.PublisherLogin = "MyPublisherLogin"
            repl.PublisherPassword = "<password>"
            repl.Publication = "MyPublication"
            repl.Subscriber = "MySubscriber"
            repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf"

            ' Create the local SQL Server Compact Edition Database subscription
            '
            repl.AddSubscription(AddOption.CreateDatabase)

            ' Synchronize to SQL Server to populate the Subscription 
            '
            repl.Synchronize()
        Catch
            ' Handle errors here
            '
        Finally
            ' Dispose the repl object
            '
            repl.Dispose()
        End Try
ISSCEMerge      *pISSCEMerge = NULL;
   ISSCEErrors  *pISSCEErrors = NULL;
   HRESULT          hr;
   BSTR            bstr = NULL;
   BOOL            fInitialized = FALSE;
   LONG            lPubChanges;
   LONG            lPubConflicts;
   LONG            lSubChanges;

   /* Create the Replication object. */
   CoCreateInstance(CLSID_Replication, NULL, CLSCTX_INPROC_SERVER,
      IID_ISSCEMerge, (LPVOID *) &pISSCEMerge);
   
  /* Set Internet properties */
   bstr = SysAllocString
     (L"https://www.adventure-works.com/sqlce/sqlcesa30.dll");
   pISSCEMerge->put_InternetURL(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"MyInternetLogin");
   pISSCEMerge->put_InternetLogin(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"<MyInternetPassword>");
   pISSCEMerge->put_InternetPassword(bstr);
   SysFreeString(bstr);
   
   /* Set Publisher properties. */
   bstr = SysAllocString(L"SamplePublisher");
   pISSCEMerge->put_Publisher(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"AdventureWorks_SQLCE");
   pISSCEMerge->put_PublisherDatabase(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"SQLCEReplDemo");
   pISSCEMerge->put_Publication(bstr);
   SysFreeString(bstr);

   pISSCEMerge->put_PublisherSecurityMode(NT_AUTHENTICATION);

   /* Set Subscriber properties. */
   bstr = SysAllocString(L"Data Source=\\Ssce.sdf");
   pISSCEMerge->put_SubscriberConnectionString(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"SQLCE Sub #1");
   pISSCEMerge->put_Subscriber(bstr);
   SysFreeString(bstr);

   /* Perform the synchronization. */
   hr = pISSCEMerge->Initialize();
   if (SUCCEEDED(hr))
      {
      fInitialized = TRUE;
      hr = pISSCEMerge->Run();
      }

   if (SUCCEEDED(hr))
      {
      pISSCEMerge->get_PublisherChanges(&lPubChanges);
      pISSCEMerge->get_PublisherConflicts(&lPubConflicts);
      pISSCEMerge->get_SubscriberChanges(&lSubChanges);
      }
   else
      {
   if(SUCCEEDED(hr = pISSCEMerge->get_ErrorRecords(&pISSCEErrors)))
      {
      ShowErrors(pISSCEErrors);
      pISSCEErrors->Release();
      };
      }

   if (fInitialized)
      {
      (void)pISSCEMerge->Terminate();
      }

请参阅

概念

使用合并复制
订阅发布 (SQL Server Compact Edition)
创建订阅

帮助和信息

获取 SQL Server Compact Edition 帮助