【发布时间】:2020-05-15 18:00:11
【问题描述】:
我在我的 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"];
请帮助我摆脱这个问题。
【问题讨论】:
标签: asp.net sql-server crystal-reports odbc database-connection