次の方法で共有


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

関連項目

その他の技術情報

リモート データ アクセスの概要

サーバーからクライアントへのデータのプル