我正在编写一个“概念验证”控制台应用程序来测试调用和检索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()
我没有设置任何DataSource,因为它们已在Crystal Report中设置。此外,我确信我的连接字符串是正确的,因为我可以用它们连接到数据源的数据库。任何帮助将不胜感激?
答案 0 :(得分:0)
我终于找到了解决这个问题的方法。我必须在程序中创建一个系统DNS - >管理工具 - >用于报告的数据库连接的ODBC DataSource。