2011-02-01 7 views
5

CR에 경험이있는 사람들에게이 질문이 표시됩니다.크리스탈 보고서 및 연결 문제

Crystal 보고서에 대한 연결 설정에 문제가 있습니다. 고객으로부터 보고서를 받았습니다. 데이터베이스의 구조는 내 서버에서 동일합니다. 그러나 서버와 db의 이름은 다릅니다. 그는 보고서 작성을 위해 "명령"을 사용하고 있습니다 (데이터베이스 필드 - 데이터베이스 전문가의 명령 추가). 이 명령에는 데이터를 반환하는 함수가 있습니다. 내 컴퓨터에서이 보고서를 실행하려고하면 TestConnectivity()를 실행하려고 할 때 문제가 발생합니다.이 메서드는 false를 반환합니다. 디버그를 시도하고 ApplyLogOnInfo() 내부 객체 RasTable에 이전 ConnectionInfo가있는 것을 발견 한 후 발견했습니다.

나는 설정된 연결에 다음 코드를 사용하고 있습니다 :

  private void ApplyConnection(ReportDocument report, ConnectionInfo connectionInfo) 
    { 
     ApplyLogOnInfo(report, connectionInfo); 
     ApplyLogOnInfoForSubreports(report, connectionInfo); 
    } 

    private void ApplyLogOnInfo(ReportDocument reportDocument, ConnectionInfo connectionInfo) 
    { 
     foreach (Table table in reportDocument.Database.Tables) 
     { 
      table.LogOnInfo.ConnectionInfo.AllowCustomConnection = true; 
      TableLogOnInfo tableLogonInfo = table.LogOnInfo; 
      tableLogonInfo.ConnectionInfo = connectionInfo; 
      table.ApplyLogOnInfo(tableLogonInfo); 

      _log.InfoFormat("Table connection state: TableName = {0}, IsConnect = {1}", table.Name, table.TestConnectivity()); 
     } 
    } 

    private void ApplyLogOnInfoForSubreports(ReportDocument reportDocument, ConnectionInfo connectionInfo) 
    { 
     Sections sections = reportDocument.ReportDefinition.Sections; 
     foreach (Section section in sections) 
     { 
      ReportObjects reportObjects = section.ReportObjects; 
      foreach (ReportObject reportObject in reportObjects) 
      { 
       _log.InfoFormat("Type = {0}, Name = {1}",reportObject.Name, reportObject.Kind); 
       if (reportObject.Kind == ReportObjectKind.SubreportObject) 
       { 
        var subreportObject = (SubreportObject)reportObject; 
        ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName); 
        ApplyLogOnInfo(subReportDocument, connectionInfo); 
       } 
      } 
     } 
    } 

그래서 제 질문은 다음과 같습니다

  • 가 어떻게 명령에 바로 연결을 설정할 수 있습니까?
  • 왜 연결을 변경할 수 없습니까? (보고서가 다른 서버에서 작성된 경우).

답변

2

나는 몇 년 전에 똑같은 문제에 직면하고 나서 수업을 썼다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using CrystalDecisions.CrystalReports.Engine; 
using CrystalDecisions.Shared; 

namespace ReportExportDemo 
{ 
    class Reports 
{ 
    static TableLogOnInfo crTableLogonInfo; 
    static ConnectionInfo crConnectionInfo; 
    static Tables crTables; 
    static Database crDatabase; 

    public static void ReportLogin(ReportDocument crDoc, string Server, string Database, string UserID, string Password) 
    { 
     crConnectionInfo = new ConnectionInfo(); 
     crConnectionInfo.ServerName = Server; 
     crConnectionInfo.DatabaseName = Database; 
     crConnectionInfo.UserID = UserID; 
     crConnectionInfo.Password = Password; 
     crDatabase = crDoc.Database; crTables = crDatabase.Tables; 

     foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables) 
     { 
      crTableLogonInfo = crTable.LogOnInfo; 
      crTableLogonInfo.ConnectionInfo = crConnectionInfo; 
      crTable.ApplyLogOnInfo(crTableLogonInfo); 
     } 
    } 

    public static void ReportLogin(ReportDocument crDoc, string Server, string Database) 
    { 
     crConnectionInfo = new ConnectionInfo(); 
     crConnectionInfo.ServerName = Server; 
     crConnectionInfo.DatabaseName = Database; 
     crConnectionInfo.IntegratedSecurity = true; 
     crDatabase = crDoc.Database; crTables = crDatabase.Tables; 

     foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables) 
     { 
      crTableLogonInfo = crTable.LogOnInfo; 
      crTableLogonInfo.ConnectionInfo = crConnectionInfo; 
      crTable.ApplyLogOnInfo(crTableLogonInfo); 
     } 
    } 
} 
} 

그냥 당신이 여기에서 자세한 내용을 사용하는 방법을 알고 싶다면 경우 : http://midnightprogrammer.net/post/Passing-Parameters-and-Loading-Crystal-Report-Programmatically.aspx

0

당신은 단지 방법 ref 단어를 추가 할 필요가

다음과 같이

을있는 정의 여기에 클래스입니다

private void ApplyConnection(ref ReportDocument report, ConnectionInfo connectionInfo) 
{ 
    ApplyLogOnInfo(report, connectionInfo); 
    ApplyLogOnInfoForSubreports(report, connectionInfo); 
} 

private void ApplyLogOnInfo(ref ReportDocument reportDocument, ConnectionInfo connectionInfo) 
{ 
    foreach (Table table in reportDocument.Database.Tables) 
    { 
     table.LogOnInfo.ConnectionInfo.AllowCustomConnection = true; 
     TableLogOnInfo tableLogonInfo = table.LogOnInfo; 
     tableLogonInfo.ConnectionInfo = connectionInfo; 
     table.ApplyLogOnInfo(tableLogonInfo); 

     _log.InfoFormat("Table connection state: TableName = {0}, IsConnect = {1}", table.Name, table.TestConnectivity()); 
    } 
} 

private void ApplyLogOnInfoForSubreports(ref ReportDocument reportDocument, ConnectionInfo connectionInfo) 
{ 
    Sections sections = reportDocument.ReportDefinition.Sections; 
    foreach (Section section in sections) 
    { 
     ReportObjects reportObjects = section.ReportObjects; 
     foreach (ReportObject reportObject in reportObjects) 
     { 
      _log.InfoFormat("Type = {0}, Name = {1}",reportObject.Name, reportObject.Kind); 
      if (reportObject.Kind == ReportObjectKind.SubreportObject) 
      { 
       var subreportObject = (SubreportObject)reportObject; 
       ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName); 
       ApplyLogOnInfo(subReportDocument, connectionInfo); 
      } 
     } 
    } 
}