C# Crystal报告和连接问题

C# Crystal报告和连接问题,c#,crystal-reports,C#,Crystal Reports,这个问题向有CR经验的人提出 我与Crystal report的设置连接有问题。 我已收到客户的报告。他和我的服务器上的数据库结构相同。但是服务器和数据库的名称不同。 他正在使用“命令”创建报告(在数据库字段->数据库专家中添加命令…)。此命令具有一些返回数据的功能。 我尝试在我的计算机上运行此报告,但在尝试执行TestConnectivity()时遇到问题。此方法返回false。 我尝试调试,发现在应用ApplyLogOnInfo()内部对象后,RasTable具有旧的ConnectionIn

这个问题向有CR经验的人提出

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

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

几年前,我曾面临同样的问题,然后写了一个类。下面是课堂:

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);
        }
    }
}
}

以防万一,如果您想知道如何使用,请在此处阅读更多内容:

您只需在方法定义中添加
ref
word即可

如下

    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);
            }
        }
    }
}