水晶报告和连接问题
问题描述:
这个问题给有经验的人。水晶报告和连接问题
我有设置连接到Crystal报告的问题。 我收到了我的客户的报告。他和我的服务器上的数据库结构相同。但服务器和数据库的名称是不同的。 他正在使用“命令”创建报告(在数据库字段 - >数据库专家中添加命令...)。这个命令有一些返回数据的功能。 我尝试在我的计算机上运行此报告,当我尝试执行TestConnectivity()时出现问题,此方法返回false。 我尝试调试,发现应用ApplyLogOnInfo()后,内部对象RasTable具有旧的ConnectionInfo。
我使用的下一个代码组连接:
private void ApplyConnection(ReportDocument report, ConnectionInfo connectionInfo)
{
ApplyLogOnInfo(report, connectionInfo);
ApplyLogOnInfoForSubreports(report, connectionInfo);
}
private void ApplyLogOnInfo(ReportDocument reportDocument, ConnectionInfo connectionInfo)
{
foreach (Table table in reportDocument.Database.Tables)
{
table.LogOnInfo.ConnectionInfo.AllowCustomConnection = true;
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
_log.InfoFormat("Table connection state: TableName = {0}, IsConnect = {1}", table.Name, table.TestConnectivity());
}
}
private void ApplyLogOnInfoForSubreports(ReportDocument reportDocument, ConnectionInfo connectionInfo)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
_log.InfoFormat("Type = {0}, Name = {1}",reportObject.Name, reportObject.Kind);
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
var subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
ApplyLogOnInfo(subReportDocument, connectionInfo);
}
}
}
}
所以我的问题是:
- 我怎样才能树立正确连接到命令?
- 为什么我无法更改连接? (如果报告是在其他服务器上创建的)。
答
我以前在几年前面对同样的问题,然后写了一个类。这里是类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace ReportExportDemo
{
class Reports
{
static TableLogOnInfo crTableLogonInfo;
static ConnectionInfo crConnectionInfo;
static Tables crTables;
static Database crDatabase;
public static void ReportLogin(ReportDocument crDoc, string Server, string Database, string UserID, string Password)
{
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);
}
}
public static void ReportLogin(ReportDocument crDoc, string Server, string Database)
{
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = Server;
crConnectionInfo.DatabaseName = Database;
crConnectionInfo.IntegratedSecurity = true;
crDatabase = crDoc.Database; crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogonInfo = crTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogonInfo);
}
}
}
}
就在,如果你想知道如何使用这里阅读更多情况:http://midnightprogrammer.net/post/Passing-Parameters-and-Loading-Crystal-Report-Programmatically.aspx
答
你只需要在方法中添加ref
字定义
如下
private void ApplyConnection(ref ReportDocument report, ConnectionInfo connectionInfo)
{
ApplyLogOnInfo(report, connectionInfo);
ApplyLogOnInfoForSubreports(report, connectionInfo);
}
private void ApplyLogOnInfo(ref ReportDocument reportDocument, ConnectionInfo connectionInfo)
{
foreach (Table table in reportDocument.Database.Tables)
{
table.LogOnInfo.ConnectionInfo.AllowCustomConnection = true;
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
_log.InfoFormat("Table connection state: TableName = {0}, IsConnect = {1}", table.Name, table.TestConnectivity());
}
}
private void ApplyLogOnInfoForSubreports(ref ReportDocument reportDocument, ConnectionInfo connectionInfo)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
_log.InfoFormat("Type = {0}, Name = {1}",reportObject.Name, reportObject.Kind);
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
var subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
ApplyLogOnInfo(subReportDocument, connectionInfo);
}
}
}
}