如何:建立 SQL Server Compact Edition 訂閱 (以程式設計的方式)
在此主題中,您將瞭解如何使用 SqlCeReplication 類別,然後執行初始資料同步處理,以便在 Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) 上建立訂閱。如需使用 SqlServerCe 命名空間的詳細資訊,請參閱在 MSDN 中的 SQL Server Compact Edition .NET 程式設計 和 SqlServerCe 命名空間參考說明文件。
若要建立 SQL Server Compact Edition 訂閱並且同步處理資料
初始化 SqlCeReplication 物件。
SqlCeReplication repl = new SqlCeReplication();
設定連接屬性。這些屬性指定您要訂閱的發行集名稱及位置、本機 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";
呼叫 AddSubscription 方法建立訂閱。
repl.AddSubscription(AddOption.CreateDatabase);
呼叫 Synchronize 方法執行資料同步處理,並使用發行集的資料填入本機資料庫。
repl.Synchronize();
範例
本範例顯示如何以 SQL Server 建立訂閱並且同步處理資料。在此範例中,本機資料庫名稱為 MyDatabase.sdf,發行集名稱為 MyPublication。
SqlCeReplication repl = null;
try
{
// Instantiate and configure 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 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/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 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);
/* 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();
請參閱
概念
使用合併式複寫
訂閱發行集 (SQL Server Compact Edition)
建立訂閱
其他資源
SQL Server Compact Edition .NET 程式設計