AddSubscription 方法
建立現有 SQL Server 發行集的新匿名訂閱。在呼叫 AddSubscription 方法之後,應用程式必須呼叫 Synchronize 方法,以根據最新的快照集,將新的訂閱與發行集同步處理。
命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)
語法
'宣告
Public Sub AddSubscription ( _
addOption As AddOption _
)
'用途
Dim instance As SqlCeReplication
Dim addOption As AddOption
instance.AddSubscription(addOption)
public void AddSubscription(
AddOption addOption
)
public:
void AddSubscription(
AddOption addOption
)
member AddSubscription :
addOption:AddOption -> unit
public function AddSubscription(
addOption : AddOption
)
參數
- addOption
型別:System.Data.SqlServerCe. . :: . .AddOption
備註
SQL Server Compact 3.5 複寫只支援匿名訂閱。
資料庫管理員必須設定 SQL Server,才能支援複寫、建立 SQL Server 發行集以及針對匿名訂閱啟用此發行集,然後任何 SQL Server Compact 3.5 應用程式才可以訂閱此發行集。管理員可使用 SQL Server 複寫的管理或程式設計介面,在 SQL Server 系統上執行這項作業。
AddOption 值會指定新建立之 SQL Server Compact 3.5 訂閱資料庫的來源。值可以是下列其中一個常數:
Value |
說明 |
---|---|
CreateDatabase |
指定必須先建立 SQL Server Compact 3.5 資料庫,然後才能透過網路向發行者取得訂閱內容。在這種情況下,AddSubscription 和 Synchronize 方法呼叫會建立 SQL Server Compact 3.5 資料庫,並從 SQL Server「發行者」下載資料庫內容。 |
ExistingDatabase |
指定資料庫已經存在,但是已經透過網路向發行者取得內容。在這種情況下,AddSubscription 和 Synchronize 方法呼叫會建立 SQL Server Compact 3.5 訂閱,並從 SQL Server「發行者」下載資料庫內容。 |
AddOption 只會影響 SQL Server Compact 3.5 資料庫最初建立的方式以及 SQL Server Compact 3.5 用戶端代理程式處理它的方式,因此會決定要將哪些資料從伺服器下載到 Windows Mobile 裝置。
範例
此範例會在呼叫 AddSubscription 方法時傳遞 AddOption 的 CreateDatabase 值,藉此建立新的訂閱資料庫。
Dim repl As SqlCeReplication = Nothing
Try
' Instantiate and configure SqlCeReplication object
'
'NOTE: when possible, prompt users to enter security
'credentials at runtime. If you store credentials in a file,
'you must secure the file to prevent unauthorized access.
'
repl = New SqlCeReplication()
repl.InternetUrl = "https://www.adventure-works.com/sqlce/sqlcesa35.dll"
repl.InternetLogin = "MyInternetLogin"
repl.InternetPassword = "<enterStrongPassword>"
repl.Publisher = "MyPublisher"
repl.PublisherDatabase = "MyPublisherDatabase"
repl.PublisherLogin = "MyPublisherLogin"
repl.PublisherPassword = "<enterStrongPassword>"
repl.Publication = "MyPublication"
repl.Subscriber = "MySubscriber"
repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf"
' Create the local SQL Mobile Database subscription
'
repl.AddSubscription(AddOption.CreateDatabase)
' Synchronize to the SQL Server to populate the Subscription
'
repl.Synchronize()
Catch
' Handle errors here
'
Finally
' Dispose the repl object
'
repl.Dispose()
End Try
SqlCeReplication repl = null;
try
{
// Instantiate and configure SqlCeReplication object
//
//NOTE: when possible, prompt users to enter security
//credentials at runtime. If you store credentials in a file,
//you must secure the file to prevent unauthorized access.
//
repl = new SqlCeReplication();
repl.InternetUrl = "https://www.adventure-works.com/sqlce/sqlcesa35.dll";
repl.InternetLogin = "MyInternetLogin";
repl.InternetPassword = "<enterStrongPassword>";
repl.Publisher = "MyPublisher";
repl.PublisherDatabase = "MyPublisherDatabase";
repl.PublisherLogin = "MyPublisherLogin";
repl.PublisherPassword = "<enterStrongPassword>";
repl.Publication = "MyPublication";
repl.Subscriber = "MySubscriber";
repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
// Create a local SQL Mobile 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();
}