如何:使用 Replication 物件建立資料庫 (以程式設計方式)
在此主題中,您將學習如何透過呼叫 SqlServerCe.Replication 物件的 AddSubscription 方法,以程式設計方式建立 Microsoft SQL Server Compact 3.5 資料庫。如需有關使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間參考文件集。
SQL Server Compact 3.5 的程序
若要使用 Replication 物件建立資料庫
初始化新的 Replication 物件。
SqlCeReplication repl = new SqlCeReplication();
設定 Replication 物件的屬性。這些屬性可包含連接到 SQL Server 發行者所需的資訊。SubscriberConnectionString 屬性指定將建立的資料庫檔名和位置。
repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf"; 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";
呼叫 AddSubscription 方法,傳入 AddOption.CreateDatabase 參數。
repl.AddSubscription(AddOption.CreateDatabase);
使用 Replication 物件建立區分大小寫的資料庫
初始化新的 Replication 物件。
SqlCeReplication repl = new SqlCeReplication();
設定 Replication 物件的屬性。這些屬性可包含連接到 SQL Server 發行者所需的資訊。SubscriberConnectionString 屬性會指定將建立之資料庫的檔案名稱和位置。此外,SubscriberConnectionString 屬性也支援新的 "Case sensitive" 屬性。這個屬性是用於建立區分大小寫的訂閱資料庫。如需有關區分大小寫之資料庫的詳細資訊,請參閱<使用定序 (SQL Server Compact)>。
repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf; LCID=1033; Case Sensitive=true"; 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";
呼叫 AddSubscription 方法,傳入 AddOption.CreateDatabase 參數。
repl.AddSubscription(AddOption.CreateDatabase);
範例
此範例顯示如何建立 Replication 物件、設定資料庫與訂閱的屬性,然後呼叫 AddSubscription 方法,以建立新的資料庫。
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 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