Web Crystal报表产生数据库登录失败 [英] Web Crystal reports produce Database logon failed
问题描述
我在我的本地机器上开发了一个Crystal报表,并且可以使用.ExportToHttpResponse在本地网站上呈现报表,但是一旦我将代码移动到生产服务器,我会收到一个数据库登录失败错误。我基本上使用一个.XML数据集文件作为源在本地开发报告。这是我的代码:
I developed a Crystal report on my local machine and I can render the report on my local website using .ExportToHttpResponse but as soon as I move my code to the production server I get a "Database logon failed" error. I basically used a .XML dataset file as the source to develop the report locally. This was my code:
我调用一个方法来呈现报表:
I call a method to render the report:
private void RenderCrystalReports(string rptName, string pdfReportName, DataSet dataForReport)
{
string reportPath = Server.MapPath(@"\App_Data\Reports\" + rptName);
ReportDocument report = new ReportDocument();
report.Load(reportPath, OpenReportMethod.OpenReportByTempCopy);
SetDBLogonForReport(ref report);
report.SetDataSource(dataForReport);
SetDBLogonForReport(ref report);
report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, true, pdfReportName);
}
并获取数据集:
private DataSet GetReportData(DateTime from, DateTime to, int userID, bool totalsOnly)
{
DataTable Job = _reportRepository.GetAccountHistoryJob(from, to, userID, totalsOnly);
Job.TableName = "Accounts";
DataSet ds = new DataSet("AccountsDS");
ds.Tables.AddRange(new DataTable[] { Job });
return ds;
}
并且就我所关注的方法来说,我连接到远程SQL服务器:
and as far as I am concerned the method below is what is supposed to help me to connect to the remote SQL server:
private void SetDBLogonForReport(ref ReportDocument reportDocument)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString());
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.DatabaseName = builder.InitialCatalog;
connectionInfo.UserID = builder.UserID;
connectionInfo.Password = builder.Password;
connectionInfo.ServerName = builder.DataSource;
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
为什么这不起作用?我想知道甚至在本地使用.xml数据集,那么如何使用我获得的sql数据集?
Why does this not work? I just figured out that even locally the .xml dataset is used so how do I use the sql dataset that I get?
推荐答案
这是为我工作的代码:
public ActionResult TestReport() {
RenderCrystalReports("demoRPT.rpt", "Some Report", GetReportDs());
return View();
}
#region Private methods
private void RenderCrystalReports(string rptName, string pdfReportName, DataSet dataForReport)
{
string reportPath = Server.MapPath(@"\Reports\" + rptName);
ReportDocument report = new ReportDocument();
report.Load(reportPath, OpenReportMethod.OpenReportByTempCopy);
DataTable jobTable = GetReportTable();
report.Database.Tables[0].SetDataSource(jobTable);
report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, true, pdfReportName);
}
private DataTable GetReportTable()
{
DataTable Job = GetReportData();
Job.TableName = "Accounts";
return Job;
}
private DataSet GetReportDs()
{
DataTable Job = GetReportData();
Job.TableName = "Accounts";
DataSet ds = new DataSet("AccountsDS");
ds.Tables.AddRange(new DataTable[] { Job });
return ds;
}
private DataTable GetReportData()
{
DataTable table = new DataTable();
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(DateTime.Now);
return table;
}
#endregion
这篇关于Web Crystal报表产生数据库登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!