Vorgehensweise: Erstellen eines Abonnements (programmgesteuert)

In diesem Thema erfahren Sie, wie Sie ein Abonnement in Microsoft SQL Server Compact 3.5 (SQL Server Compact 3.5) mithilfe der SqlCeReplication-Klasse erstellen und die erste Datensynchronisierung ausführen.

Verfahren für SQL Server Compact 3.5

So erstellen Sie ein Abonnement und synchronisieren Daten

  1. Initialisieren Sie ein SqlCeReplication-Objekt.

    SqlCeReplication repl = new SqlCeReplication();
    
  2. Legt die Verbindungseigenschaften fest. Diese Eigenschaften geben den Namen und Speicherort der Veröffentlichung an, die Sie abonnieren, den Namen und Speicherort der lokalen SQL Server Compact 3.5-Datenbank sowie den Speicherort des Server-Agents für SQL Server Compact 3.5.

    repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa35.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. Rufen Sie die AddSubscription-Methode auf, um das Abonnement zu erstellen.

    repl.AddSubscription(AddOption.CreateDatabase);
    
  4. Rufen Sie die Synchronize-Methode auf, um die Datensynchronisierung auszuführen und die lokale Datenbank mit den Daten aus der Veröffentlichung aufzufüllen.

    repl.Synchronize();
    

Beispiel

In diesem Beispiel wird veranschaulicht, wie ein Abonnement erstellt und Daten mit SQL Server synchronisiert werden. In diesem Beispiel trägt die lokale Datenbank die Bezeichnung MyDatabase.sdf und die Veröffentlichung die Bezeichnung MyPublication.

SqlCeReplication repl = null;

try
{
   // Instantiate and configure SqlCeReplication object
   //
   repl = new SqlCeReplication();
   repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa35.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 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
   ' Instantiate and configure SqlCeReplication object

   repl = New SqlCeReplication()
   repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa35.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 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/sqlcesa35.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);

   /* Create the new anonymous subscription. */
   pISSCEMerge->AddSubscription(CREATE_DATABASE);

   /* Perform the first synchronization to download the initial  
          replica. */
   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();
      }

Siehe auch

Andere Ressourcen

Verwenden der Mergereplikation
Abonnieren von Veröffentlichungen (SQL Server Compact)
Erstellen eines Abonnements
System.Data.SqlServerCe Namespace Overview

Hilfe und Informationen

Informationsquellen (SQL Server Compact 3.5 Service Pack 1)