如何请求数据(以编程方式)
本主题将介绍如何通过使用 SqlCeRemoteDataAccess 类从 Microsoft SQL Server 数据库请求数据,并填充到 Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) 数据库。有关使用 SqlServerCe 命名空间的详细信息,请参阅 SqlServerCe 命名空间参考文档。
使用远程数据访问请求数据
初始化 SqlCeRemoteDataAccess 对象,并设置连接的属性。
SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", "MyLogin", "<password>", "Data Source=MyDatabase.sdf");
调用 Pull 方法,传入要从中请求数据的 SQL Server 表的名称、要使用的 SELECT 语句以及指向本地 SQL Server Compact Edition 数据库的连接字符串。您还可以指定要使用的跟踪选项和用于记录远程数据访问 (RDA) 错误的错误表的位置。
rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes, "ErrorTable");
示例
此示例显示如何从 SQL Server 数据库上的 DimEmployee 表请求数据,并填充名为 Employees 的 SQL Server Compact Edition 表。
string rdaOleDbConnectString = @"Provider=SQLOLEDB; Data Source=MySqlServer;
Initial Catalog=AdventureWorks; User Id=username;
Password = <password>";
// Initialize RDA Object
//
SqlCeRemoteDataAccess rda = null;
try
{
// Try the Pull Operation
//
rda = new SqlCeRemoteDataAccess(
"https://www.adventure-works.com/sqlmobile/sqlcesa30.dll",
"MyLogin",
"<password>",
"Data Source=MyDatabase.sdf");
rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString,
RdaTrackOption.TrackingOnWithIndexes, "ErrorTable");
// or, try one of these overloads:
//
// rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString,
// RdaTrackOption.TrackingOnWithIndexes);
//
// rda.Pull("Employees", "SELECT * FROM DimEmployee", 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 Pull Operation
'
rda = New SqlCeRemoteDataAccess( _
"https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", _
"MyLogin", _
"<password>", _
"Data Source=MyDatabase.sdf")
rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString, _
RdaTrackOption.TrackingOnWithIndexes, "ErrorTable")
' or, try one of these overloads:
' rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString, _
' RdaTrackOption.TrackingOnWithIndexes)
'
' rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString)
Catch
' Handle errors here
'
Finally
' Dispose of the RDA object
'
rda.Dispose()
End Try