Creating a Subscription

Before you can create a subscription, you must configure Microsoft SQL Server replication, create the SQL Server publication, and configure Microsoft Internet Information Services (IIS). For information about this process, see Implementing Replication (SQL Server Compact Edition) and Configure Web Synchronization Wizard Help.

After the publication is created and IIS is correctly configured, you can create a SQL Server 2005 Compact Edition (SQL Server Compact Edition) subscription by using either of these methods:

Calling the AddSubscription Method

SQL Server Compact Edition applications use the Replication object to programmatically create a subscription and download that subscription to a SQL Server Compact Edition database on a Microsoft smart device. The application does this by creating the Replication object, setting the appropriate Replication object properties, and calling the AddSubscription method. The examples in this topic show how you do this.

Multiple Subscriptions and the AddSubscription Method

Because SQL Server Compact Edition supports synchronizing a single database with multiple publications, you must consider the effects of multiple subscriptions when you use the AddSubscription method.

There are two options used when calling the AddSubscription method: CreateDatabase and ExistingDatabase. The CreateDatabase option is not affected when you use multiple subscriptions, but the ExistingDatabase option is. When you use ExistingDatabase, you must supply values for the Publisher, Publication, and PublisherDatabase properties before calling the AddSubscription method.

Copying an Existing Subscription

You can create a new subscription by copying an existing subscription from one smart device to another. This makes it easy to deploy a smart device application on many devices when all the Subscribers are using the same publication.

You can create the initial subscription on one smart device and then deploy the application by copying the application and the initial subscription to as many devices as you want. When the application first synchronizes, SQL Server Compact Edition automatically recognizes it as a new Subscriber, due to the new device and/or physical folder location change, and creates a new subscription. By copying the initial subscription to a device, you avoid downloading the initial subscription to the device over the network.

Note

SQL Server Compact Edition supports external storage devices, including Compact Flash memory and drives. An efficient way to deploy large SQL Server Compact Edition databases is to distribute them on these storage devices. However, these storage devices have relatively long access times when compared to the smart device's RAM. Using these devices might affect the performance of your application

Visual C++ for Devices

   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);

   /* 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();

   

See Also

Concepts

Supporting Multiple Subscriptions
Dropping a Subscription
Reinitializing a Subscription (SQL Server Compact Edition)

Help and Information

Getting SQL Server Compact Edition Assistance