다음을 통해 공유


보고서 로그온 코드 추가

이제 코드 숨김 클래스에 로그온 코드를 추가할 준비가 되었습니다. 이후의 작업은 SetDBLogonForReport()라는 private 도우미 메서드를 만드는 것으로 시작합니다.

SetDBLogonForReport() 메서드를 만들고 코딩하려면

  1. 이 Web Form 또는 Windows Form에 대한 코드 숨김 클래스로 돌아옵니다.

  2. ConnectionInfo와 ReportDocument라는 두 개의 매개 변수를 가지는 새 private 메서드 SetDBLogonForReport()를 클래스 맨 아래에 만듭니다.

    Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
    
    End Sub
    
    private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
    {
    }
    
  3. 이 메서드 내에서 ReportDocument 매개 변수의 Database 속성에 대한 Tables 속성으로부터 Tables 인스턴스를 검색합니다.

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\e2c9s1d7.alert_note(ko-kr,VS.90).gif" alt="Note" class="note" />참고</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>Tables는 Table 클래스의 인스턴스를 포함하는 인덱싱된 클래스입니다.</p></td>
</tr>
</tbody>
</table>

``` vb
Dim myTables As Tables = myReportDocument.Database.Tables
```

``` csharp
Tables tables = reportDocument.Database.Tables;
```
  1. 인덱싱된 Tables 클래스 인스턴스에 있는 각 Table 인스턴스를 반복하는 foreach 루프를 만듭니다.
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\e2c9s1d7.alert_note(ko-kr,VS.90).gif" alt="Note" class="note" />참고</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>System.Web.UI.WebControls 네임스페이스의 Table 클래스와 구분하려면 Table 클래스의 전체 네임스페이스 경로를 포함해야 합니다.</p></td>
</tr>
</tbody>
</table>

``` vb
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables

Next
```

``` csharp
foreach(CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
}
```
  1. foreach 루프를 사용하여 Table 인스턴스의 LogOnInfo 속성에서 TableLogonInfo 인스턴스를 검색합니다.

    Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
    
    TableLogOnInfo tableLogonInfo = table.LogOnInfo;
    
  2. foreach 루프 내에서 TableLogonInfo의 ConnectionInfo 속성을 ConnectionInfo 매개 변수로 설정합니다.

    myTableLogonInfo.ConnectionInfo = myConnectionInfo
    
    tableLogonInfo.ConnectionInfo = connectionInfo;
    
  3. foreach 루프 내에서 TableLogonInfo 인스턴스를 Table 인스턴스의 ApplyLogonInfo 메서드에 매개 변수로 전달합니다.

    myTable.ApplyLogOnInfo(myTableLogonInfo)
    
    table.ApplyLogOnInfo(tableLogonInfo);
    

이 단계에서는 데이터베이스에 대해 로그온을 설정하는 메서드를 만들었습니다. 그러나 보고서에 데이터베이스 로그온 정보가 있음을 알리기 위해서는 이 메서드를 처리하도록 ConfigureCrystalReports() 메서드를 수정해야 합니다.

ConfigureCrystalReports() 메서드를 수정하는 데는 다음과 같은 두 가지 작업이 필요합니다.

  • ConnectionInfo 인스턴스 구성
  • SetDBLogonForReport() 메서드 호출

데이터베이스 로그온 코드를 처리하도록 ConfigureCrystalReports() 메서드를 수정하려면

  1. ConfigureCrystalReports() 메서드의 코드에서 보고서를 CrystalReportViewer 컨트롤에 바인딩하는 줄 위에 두 개의 줄 바꿈을 삽입합니다.

  2. 이 두 줄 사이에서 ConnectionInfo 클래스를 선언하고 인스턴스화합니다.

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\e2c9s1d7.alert_note(ko-kr,VS.90).gif" alt="Note" class="note" />참고</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>ConnectionInfo 클래스에 액세스하려면 CrystalDecisions.Shared 네임스페이스에 대한 &quot;Imports&quot; [Visual Basic] 또는 &quot;using&quot; [C#] 문을 코드 숨김 클래스 위에 포함하십시오. 이 선언은 <a href="ms227453(v=vs.90).md">&quot;프로젝트 설정&quot;</a> 과정에서 추가했습니다.</p></td>
</tr>
</tbody>
</table>

``` vb
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
```

``` csharp
ConnectionInfo connectionInfo = new ConnectionInfo();
```
  1. ConnectionInfo 인스턴스의 DatabaseName, UserID 및 Password 속성을 설정합니다.
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\e2c9s1d7.alert_note(ko-kr,VS.90).gif" alt="Note" class="note" />참고</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>보안을 위해 제한된 액세스 권한을 가진 데이터베이스 계정을 사용해야 합니다. 자세한 내용은 <a href="ms227498(v=vs.90).md">&quot;보안: 제한된 액세스 데이터베이스 계정 만들기&quot;</a>를 참조하십시오.</p></td>
</tr>
</tbody>
</table>

코드를 직접 작성할 때는 아래에서 암호의 예로 사용하고 있는 1234를 사용자의 고유한 암호로 바꿔야 합니다.

``` vb
myConnectionInfo.DatabaseName = "Northwind"
myConnectionInfo.UserID = "limitedPermissionAccount"
myConnectionInfo.Password = "
1234"
```

``` csharp
connectionInfo.DatabaseName = "Northwind";
connectionInfo.UserID = "limitedPermissionAccount";
connectionInfo.Password = "
1234";
```
  1. ConnectionInfo 인스턴스와 NorthwindCustomers 보고서를 전달하여 SetDBLogonForReport() 메서드를 호출합니다.
``` vb
SetDBLogonForReport(myConnectionInfo, northwindCustomersReport)
```

``` csharp
SetDBLogonForReport(connectionInfo, northwindCustomersReport);
```

이 뒤에는 보고서를 CrystalReportViewer 컨트롤에 바인딩하는 원래 코드가 이어집니다.

이제 프로젝트를 빌드하고 실행할 준비가 되었습니다. 데이터베이스에 로그온하기 위한 코드를 추가했으므로 보고서가 올바르게 로드되어야 합니다.

NorthwindCustomers 보고서가 로드되는지 테스트하려면

  1. 빌드 메뉴에서 솔루션 빌드를 선택합니다.

  2. 빌드 오류가 발생하면 바로 수정합니다.

  3. 디버그 메뉴에서 시작을 클릭합니다.

    이제 NorthwindCustomers 보고서가 제대로 표시됩니다.

  4. Visual Studio로 돌아간 다음 중지를 클릭하여 디버그 모드를 종료합니다.

이제 NorthwindCustomers 보고서가 제대로 표시됩니다.