Crystal报表和连接问题

时间:2011-02-01 15:05:31

标签: c# crystal-reports

对于有CR经验的人来说,这个问题。

我遇到设置Crystal报表连接的问题。 我收到了客户的报告。数据库的结构在他和我的服务器上是相同的。但是服务器和数据库的名称是不同的。 他正在使用“命令”来创建报告(在数据库字段中添加命令 - >数据库专家...)。该命令具有一些返回数据的功能。 我尝试在我的计算机上运行此报告,当我尝试执行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);
                }
            }
        }
    }

所以我的问题是:

  • 如何设置与Command的正确连接?
  • 为什么我不能改变连接? (如果报告是在其他服务器上创建的)。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
            }
        }
    }
}