방법: 구독 만들기(프로그래밍 방식)
이 항목에서는 SqlCeReplication 클래스를 사용하여 Microsoft SQL Server Compact 3.5(SQL Server Compact 3.5)에서 구독을 만든 다음 초기 데이터 동기화를 수행하는 방법에 대해 설명합니다.
SQL Server Compact 3.5의 절차
구독 및 데이터 동기화를 만들려면
SqlCeReplication 개체를 초기화합니다.
SqlCeReplication repl = new SqlCeReplication();
연결 속성을 설정합니다. 이러한 속성은 구독하고 있는 게시의 이름 및 위치, 로컬 SQL Server Compact 3.5 데이터베이스의 이름 및 위치, 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";
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/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();
}
참고 항목
관련 자료
병합 복제 사용
게시 구독(SQL Server Compact)
구독 만들기
System.Data.SqlServerCe Namespace Overview