In this topic, you will learn how to pull data down from a Microsoft SQL Server database to a Microsoft SQL Server Compact 3.5 database by using the SqlCeRemoteDataAccess class. For more information about using the SqlServerCe namespace, see the SqlServerCe namespace reference documentation.
To pull data by using remote data access
Initialize a SqlCeRemoteDataAccess object and set the properties for the connection.
SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa35.dll", "MyLogin", "<enterStrongPasswordHere>", "Data Source=MyDatabase.sdf");
Call the Pull method, passing in the name of the SQL Server table from which to pull the data, the SELECT statement to use, and the connection string to the local SQL Server Compact 3.5 database. You can also specify the tracking option to use and the location of the error table for recording remote data access (RDA) errors.
rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes, "ErrorTable");
Example
This sample shows how to pull data from the DimEmployee table on a SQL Server database and populate a SQL Server Compact 3.5 table named Employees.
string rdaOleDbConnectString = @"Provider=SQLOLEDB; Data Source=MySqlServer;
Initial Catalog=AdventureWorks; User Id=username;
Password = <enterStrongPasswordHere>";
// Initialize RDA Object
//
SqlCeRemoteDataAccess rda = null;
try
{
// Try the Pull Operation
//
rda = new SqlCeRemoteDataAccess(
"https://www.adventure-works.com/sqlmobile/sqlcesa35.dll",
"MyLogin",
"<enterStrongPasswordHere>",
"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 = <enterStrongPasswordHere>"
' Initialize RDA Object
'
Dim rda As SqlCeRemoteDataAccess = Nothing
Try
' Try the Pull Operation
'
rda = New SqlCeRemoteDataAccess( _
"https://www.adventure-works.com/sqlmobile/sqlcesa35.dll", _
"MyLogin", _
"<enterStrongPasswordHere>", _
"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