我在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;
}
}
我上次调用的函数是create pdf:
public MemoryStream asPdf
{
get
{
using (TempMemoryStream = (MemoryStream)reportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat))
{
return TempMemoryStream;
}
}
}
谢谢建议,帮帮我PLZ
答案 0 :(得分:1)
尝试此代码
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
crystalReport.SetDatabaseLogon("username", "password", @"server name", "DB name");
CrystalReportViewer1.ReportSource = crystalReport;
希望它可以帮助你。
答案 1 :(得分:1)
我转换了我的代码,就像这个样本一样有效!
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);
}
}
}
答案 2 :(得分:0)
private void LoadReport()
{
doc = new ReportDocument();
doc.Load(Server.MapPath("CrSalesReport.rpt"));
doc.SetDatabaseLogon(AppConfig.ReportServerDSUserName, AppConfig.ReportServerDSPassword, AppConfig.ReportServerDomain, "TexERP", false);
}
尝试使用此代码进行数据库登录