다음을 통해 공유


방법: 데이터 끌어오기(프로그래밍 방식)

이 항목에서는 SqlCeRemoteDataAccess 클래스를 사용하여 Microsoft SQL Server 데이터베이스의 데이터를 Microsoft SQL Server 2005 Compact Edition(SQL Server Compact Edition) 데이터베이스로 끌어오는 방법에 대해 설명합니다. SqlServerCe 네임스페이스 사용에 대한 자세한 내용은 SqlServerCe 네임스페이스 참조 설명서를 참조하십시오.

RDA(Remote Data Access)를 사용하여 데이터를 끌어오려면 다음과 같이 하십시오.

  1. SqlCeRemoteDataAccess 개체를 초기화하고 연결 속성을 설정합니다.

    SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", "MyLogin", "<password>", "Data Source=MyDatabase.sdf");
    
  2. 데이터를 끌어올 SQL Server 테이블의 이름, 사용할 SELECT 문, 로컬 SQL Server Compact Edition 데이터베이스에 대한 연결 문자열을 전달하여 Pull 메서드를 호출합니다. 사용할 추적 옵션 및 RDA(Remote Data Access) 오류를 기록할 오류 테이블의 위치도 지정할 수 있습니다.

    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

참고 항목

개념

RDA(Remote Data Access) 소개
서버에서 클라이언트로 데이터 끌어오기

도움말 및 정보

SQL Server Compact Edition 지원 정보 보기