如何:提取資料 (以程式設計的方式)
在此主題中,您將學習到如何使用 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