0

我正在编写一个“概念验证”控制台应用程序来测试调用和检索 Crystal Reports。我不想使用 ReportViewer 而是通过流检索报告。我正在使用 CRforVS redist 库 CrystalDecisions.CrystalReports.Engine 和 CrystalDecisions.Shared。我的代码如下所示:

static void Main(string[] args)
{
    var x = new ReportDocument();
    x.Load("[REPORT PATH]");
    x.SetParameterValue("[PARAM NAME]", "[PARAM VALUE]");
    // set parameter values

    SetDbLoginForReport(x);

    Sections sections = x.ReportDefinition.Sections;
    foreach (Section section in sections)
    {
        ReportObjects reportObjects = section.ReportObjects;
        foreach (ReportObject reportObject in reportObjects)
        {
            if (reportObject.Kind == ReportObjectKind.SubreportObject)
            {
                SubreportObject subreportObject = (SubreportObject)reportObject;
                ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
                SetDbLoginForReport(subReportDocument);
            }
        }
    }

    var s = x.ExportToStream(ExportFormatType.CrystalReport);

}

private static void SetDbLoginForReport(ReportDocument document)
{
    foreach (var table in document.Database.Tables)
    {
        var conn = new ConnectionInfo();
        var myTable = table as Table;
        var myTableLogonInfo = myTable.LogOnInfo;
        conn.ServerName = "";
        conn.DatabaseName = "";
        conn.UserID = "";
        conn.Password = "";
        myTableLogonInfo.ConnectionInfo = conn;
        myTable.ApplyLogOnInfo(myTableLogonInfo);
    }
}

ExportToStream 给出错误消息“无法打开连接”。并引发以下异常:

   at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)
   at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
   at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)
   at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportOptions options)
   at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportFormatType formatType)
   at ConsoleApplication2.Program.Main(String[] args) in C:\Users\paul.welbourne\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 41
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

我没有设置任何数据源,因为它们已经在 Crystal Report 中设置。另外,我确信我的连接字符串是正确的,因为我可以使用它们连接到数据源的数据库。任何帮助,将不胜感激?

4

1 回答 1

0

我终于找到了解决这个问题的方法。我必须在程序 -> 管理工具 -> ODBC 数据源中为报表的数据库连接创建一个系统 DNS。

于 2013-08-21T16:22:04.280 回答