如何推送数据(以编程方式)
本主题将介绍如何通过使用 SqlCeRemoteDataAccess 类将数据从 Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) 数据库推送到 Microsoft SQL Server 数据库。有关使用 SqlServerCe 命名空间的详细信息,请参阅 SqlServerCe 命名空间参考文档。
使用远程数据访问推送数据
初始化 SqlCeRemoteDataAccess 对象,并设置连接的属性。
SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", "Data Source=MyDatabase.sdf");
调用 Push 方法,传入本地 SQL Server Compact Edition 表的名称(从其中推送数据)以及与 SQL Server 数据库之间的连接字符串。还可以指定要使用的批处理选项。
rda.Push("MyLocalTable", rdaOleDbConnectString, RdaBatchOption.BatchingOn);
示例
下面的示例说明如何将 SQL Server Compact Edition 数据库上 MyLocalTable 表中的数据推送到名为 MySqlServer 的 SQL Server 实例上的 AdventureWorks 数据库中。
string rdaOleDbConnectString = @"Provider=SQLOLEDB; Data Source=MySqlServer;
Initial Catalog=AdventureWorks; User Id=username;
Password = <password>";
// Initialize RDA Object
//
SqlCeRemoteDataAccess rda = null;
try
{
// Try the Push Operation
//
rda = new SqlCeRemoteDataAccess(
"https://www.adventure-works.com/sqlmobile/sqlcesa30.dll",
"Data Source=MyDatabase.sdf");
rda.InternetLogin = "MyLogin";
rda.InternetPassword = "<password>";
rda.Push("MyLocalTable", rdaOleDbConnectString, RdaBatchOption.BatchingOn);
// or, try this overload:
//
// rda.Push("MyLocalTable", rdaOleDbConnectString);
}
catch (SqlCeException)
{
// Handle errors here
//
}
finally
{
// Dispose of the RDA Object
//
rda.Dispose();
}
Dim rdaOleDbConnectString As String = _
"Provider=SQLOLEDB; "Data Source=MySqlServer;Initial Catalog=AdventureWorks; "
"User Id=username;Password = <password>"
' Initialize RDA Object
'
Dim rda As SqlCeRemoteDataAccess = Nothing
Try
' Try the Push Operation
'
rda = New SqlCeRemoteDataAccess( _
"https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", _
"Data Source=MyDatabase.sdf")
rda.InternetLogin = "MyLogin"
rda.InternetPassword = "<password>"
rda.Push("MyLocalTable", rdaOleDbConnectString, RdaBatchOption.BatchingOn)
' or, try this overload:
'
' rda.Push("MyLocalTable", rdaOleDbConnectString)
Catch
' Handle errors here
'
Finally
' Dispose of the RDA Object
'
rda.Dispose()
End Try