![](/img/trans.png)
[英]Crystal Reports failed to open connection - at ReportDocument.Load stage.
[英]Crystal Reports load reportDocument failed
我在vs 2010 c#上使用水晶報表,並使用CR的rpt文檔創建pdf文件。
我將此代碼放在Windows服務上,我的代碼正常工作30-40次,但隨后每增加+5 +7,內存就會增加。
最后我得到這樣的錯誤:加載文件失敗!
我的代碼:(我認為我可以處置/關閉conn,但是如何)
private void ReportLogin(ReportDocument crDoc, string Database, string Server, string UserID, string Password)
{
try
{
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = Server;
crConnectionInfo.DatabaseName = Database;
crConnectionInfo.UserID = UserID;
crConnectionInfo.Password = Password;
crDatabase = crDoc.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogonInfo = crTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogonInfo);
}
}
catch (Exception x)
{
throw x;
}
}
private void _CrystalReport(string RptFilePath)
{
reportDocument = LoadDoc(RptFilePath);
RptParamsWithType = new Dictionary<string, string>();
if (reportDocument.ParameterFields.Count > 0)
{
foreach (ParameterField pField in reportDocument.ParameterFields)
{
RptParamsWithType.Add(pField.Name, pField.ParameterValueType.ToString().Replace("Parameter", ""));
}
}
}
加載功能:
private ReportDocument LoadDoc(string RptFilePath)
{
try
{
reportDocument = new ReportDocument();
reportDocument.Load(RptFilePath);
return reportDocument;
}
catch (Exception x)
{
throw x;
}
}
我最后一次調用的函數是創建pdf:
public MemoryStream asPdf
{
get
{
using (TempMemoryStream = (MemoryStream)reportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat))
{
return TempMemoryStream;
}
}
}
謝謝忠告,請幫助我
試試這個代碼
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
crystalReport.SetDatabaseLogon("username", "password", @"server name", "DB name");
CrystalReportViewer1.ReportSource = crystalReport;
希望對您有幫助。
我像這樣的示例轉換了我的代碼,它可以工作!
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using CrystalDecisions;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
namespace Test.Utilities
{
public class ReportFactory
{
protected static Queue reportQueue = new Queue();
protected static ReportClass CreateReport(Type reportClass)
{
object report = Activator.CreateInstance(reportClass);
reportQueue.Enqueue(report);
return (ReportClass)report;
}
public static ReportClass GetReport(Type reportClass)
{
//75 is my print job limit.
if (reportQueue.Count > 75) ((ReportClass)reportQueue.Dequeue()).Dispose();
return CreateReport(reportClass);
}
}
}
private void LoadReport()
{
doc = new ReportDocument();
doc.Load(Server.MapPath("CrSalesReport.rpt"));
doc.SetDatabaseLogon(AppConfig.ReportServerDSUserName, AppConfig.ReportServerDSPassword, AppConfig.ReportServerDomain, "TexERP", false);
}
嘗試使用此代碼進行數據庫登錄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.