我在asp.net Web应用程序中使用Visual Studio 2013,并大量使用Crystal Reports。我的数据库是SQL Server(使用AWS RDS)。一切工作正常。从数据库方面来看,唯一的问题是,即使关闭浏览器窗口后,Crystal Report连接也不会关闭/处分。它不断增加连接数。
这是我的代码:
ReportDocument cryRpt = new ReportDocument();
ParameterFields paramFields = new ParameterFields();
ParameterField paramField = new ParameterField();
cryRpt.Load(Server.MapPath("~/Reports/Report001.rpt"));
String host = System.Configuration.ConfigurationManager.AppSettings["SqlServer"];
String database = System.Configuration.ConfigurationManager.AppSettings["SqlDatabase"];
String user = System.Configuration.ConfigurationManager.AppSettings["SqlUsername"];
String password = System.Configuration.ConfigurationManager.AppSettings["SqlPassword"];
var connectionInfo = new ConnectionInfo
{
Type = ConnectionInfoType.SQL,
ServerName = host,
DatabaseName = database
};
connectionInfo.IntegratedSecurity = false;
connectionInfo.UserID = user;
connectionInfo.Password = password;
TableLogOnInfo newLogonInfo = null;
foreach (CrystalDecisions.CrystalReports.Engine.Table currentTable in cryRpt.Database.Tables)
{
newLogonInfo = currentTable.LogOnInfo;
newLogonInfo.ConnectionInfo = connectionInfo;
currentTable.ApplyLogOnInfo(newLogonInfo);
}
ParameterField pReportName = new ParameterField();
pReportName.ParameterFieldName = "REPONAME";
ParameterDiscreteValue dcpReportName = new ParameterDiscreteValue();
dcpReportName.Value = "REPORT";
pReportName.CurrentValues.Add(dcpReportName);
paramFields.Add(pReportName);
CrystalReportViewer1.ParameterFieldInfo = paramFields;
CrystalReportViewer1.Zoom(100);
CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
CrystalReportViewer1.ReportSource = cryRpt;
CrystalReportViewer1.ReuseParameterValuesOnRefresh = true;
CrystalReportViewer1.ShowFirstPage();
// Disposing the report
foreach (CrystalDecisions.CrystalReports.Engine.Table currentTable in cryRpt.Database.Tables)
{
currentTable.Dispose();
}
CrystalReportViewer1.ReportSource = null;
cryRpt.Database.Dispose();
cryRpt.Close();
cryRpt.Dispose();
cryRpt = (ReportDocument)CrystalReportViewer1.ReportSource;
CrystalReportViewer1.Dispose();
connectionInfo.Attributes.Collection.Clear();
GC.Collect();
也试图像这样使用卸载方法。但没有运气。
protected void CrystalReportViewer1_Unload(object sender, EventArgs e)
{
cryRpt.Close();
cryRpt.Dispose();
CrystalReportViewer1.Dispose();
}
作为一种临时解决方案,我正在使用存储过程从数据库中手动终止睡眠的数据库连接。
我正在使用ODBC连接来获取数据。 ODBC凭据存储在配置文件中,并按以下方式检索。
String host = System.Configuration.ConfigurationManager.AppSettings["SqlServer"];
String database = System.Configuration.ConfigurationManager.AppSettings["SqlDatabase"];
String user = System.Configuration.ConfigurationManager.AppSettings["SqlUsername"];
String password = System.Configuration.ConfigurationManager.AppSettings["SqlPassword"];
请帮助我解决这个问题。
我认为您的问题出在数据库连接本身,而与应用程序无关,而与Crystal Reports组件无关。尝试在放置查看器CrystalReportViewer1.Dispose();后关闭连接Connection.Close