共用方式為


如何:使用 RDA 物件提取資料 (以程式設計的方式)

在此主題中,您將學習如何使用 SqlCeRemoteDataAccess 類別,將 Microsoft SQL Server 資料庫中的資料提取到 Microsoft SQL Server Compact 3.5 資料庫。如需有關使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間參考文件集。

若要使用遠端資料存取來提取資料

  1. 初始化 SqlCeRemoteDataAccess 物件,並設定連接的屬性。

    SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa35.dll", "MyLogin", "<enterStrongPasswordHere>", "Data Source=MyDatabase.sdf");
    
  2. 呼叫 Pull 方法,以做為提取資料來源的 SQL Server 資料表名稱來傳遞,並呼叫要使用的 SELECT 陳述式,以及對本機 SQL Server Compact 3.5 資料庫的連接字串。您也可以指定要使用的追蹤選項,以及用來記錄遠端資料存取 (RDA) 錯誤的錯誤資料表位置。

    rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes, "ErrorTable");
    

範例

此範例顯示如何從 SQL Server 資料庫上的 DimEmployee 資料表提取資料,並填入名為 Employees 的 SQL Server Compact 3.5 資料表。

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

請參閱

其他資源

遠端資料存取簡介

將資料從伺服器提取到用戶端