【发布时间】:2014-05-16 04:17:14
【问题描述】:
我编写了一个小型 C# 应用程序来使用 Crystal Reports 将报表保存为 pdf。我遇到的问题是,当我保存更多报告时,我的应用程序的句柄不断增加,并且我可以看到在保存每个报告后建立新的数据库连接并保持打开状态。最终,应用程序从 Crystal 那里得到一个异常,说“数据库登录错误”,或者我收到一个 C++ 运行时错误,抱怨“R6025:纯虚函数调用”。
根据this technique,我正在使用process explorer 查看应用程序句柄。我正在使用 MS SQL Server 的活动监视器检查数据库连接。每个保存的报告都会导致另外 100 个打开的“信号量”和“事件”句柄以及 2 个数据库连接。
我相信我通过调用 Close() 然后 Dispose() 正确地处理了报告,如 here 所述。网络上的其他建议包括手动关闭数据库连接 (Crystal reports - close the database connection) 和调用 GC.Collect(),但在我的情况下都没有。
一些环境细节
- Visual Studio 2012
- 面向 .NET 4.5.1 运行时的 C# 控制台应用程序
- 数据库:MSSQL Server 2012
- .NET Framework 4.0 版本 13.09.1312 的水晶报表
- 使用 SAP 数据库连接的 Crystal Report 文档(这很重要 - 请参阅答案)
这是一个显示相同问题的示例应用程序:
using System;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace ExampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
while (true)
{
SaveReport();
}
}
static void SaveReport()
{
Console.WriteLine("loading report...");
ReportDocument rpt = new ReportDocument();
rpt.Load("test.rpt");
rpt.SetDatabaseLogon("username", "password");
foreach (IConnectionInfo info in rpt.DataSourceConnections)
{
info.IntegratedSecurity = false;
info.SetConnection("SQL", "our_database", "username", "password");
}
rpt.ExportToDisk(ExportFormatType.PortableDocFormat, "test.pdf");
WaitForKeypress("about to dispose report");
// attempt to manually close tables / database links
// none of the following commented code has had any effect
// foreach (TableLink tl in rpt.Database.Links)
// {
// tl.Dispose();
// }
// rpt.Database.Links.Reset();
// rpt.Database.Links.Dispose();
// foreach (Table table in rpt.Database.Tables)
// {
// table.Dispose();
// }
// rpt.Database.Tables.Reset();
// rpt.Database.Tables.Dispose();
// rpt.DataSourceConnections.Clear();
// rpt.Database.Dispose();
rpt.Close();
rpt.Dispose();
// rpt = null;
// GC.Collect();
WaitForKeypress("disposed");
}
private static void WaitForKeypress(string msg = "press a key...")
{
Console.WriteLine(msg);
Console.ReadLine();
}
}
}
谁能告诉我我做错了什么?
【问题讨论】:
标签: c# sql-server crystal-reports