运行显示包含子报表的水晶报表的应用程序时,系统会提示用户输入登录信息。
下面是我用来将登录信息传递给报告的代码。显示表单时执行此代码。我有一个测试vs2017应用程序,其中包含带有水晶报表查看器的表单。登录信息已传递并且可以正常工作,直到添加子报表为止。将子报表添加到我现有的报表后,系统将提示用户登录数据库。输入正确的登录凭据后,将显示登录失败消息。该报告无法运行。该报告从数据库的命令查询中获取数据。
Sections crSections;
ReportDocument crReportDocument, crSubreportDocument;
SubreportObject crSubreportObject;
ReportObjects crReportObjects;
ConnectionInfo crConnectionInfo;
Database crDatabase;
Tables crTables;
TableLogOnInfo crTableLogOnInfo;
crReportDocument = new ReportDocument();
crReportDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CrystalReport3.rpt"));
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = "myservname";
crConnectionInfo.DatabaseName = "mydatabasename";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "myusername";
foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
{
crTableLogOnInfo = aTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogOnInfo);
}
// THIS STUFF HERE IS FOR REPORTS HAVING SUBREPORTS
// set the sections object to the current report's section
crSections = crReportDocument.ReportDefinition.Sections;
// loop through all the sections to find all the report objects
foreach (Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
//loop through all the report objects in there to find all
子报表 foreach(crReportObjects中的ReportObject crReportObject) { 如果(crReportObject.Kind == ReportObjectKind.SubreportObject) { crSubreportObject =(SubreportObject)crReportObject; //打开子报表对象并按照常规报表登录 crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName); crDatabase = crSubreportDocument.Database; crTables = crDatabase.Tables; foreach(crTables中的CrystalDecisions.CrystalReports.Engine.Table aaTable) { crTableLogOnInfo = aaTable.LogOnInfo; crTableLogOnInfo.ConnectionInfo = crConnectionInfo; aaTable.ApplyLogOnInfo(crTableLogOnInfo);
}
}
}
}
crystalReportViewer1.ReportSource = crReportDocument;
crystalReportViewer1.Refresh();
我希望在传递登录凭据时运行报告而不会提示用户。
答案 0 :(得分:0)
尝试一下:Link
代码:
Sections crSections;
ReportDocument crReportDocument, crSubreportDocument;
SubreportObject crSubreportObject;
ReportObjects crReportObjects;
ConnectionInfo crConnectionInfo;
Database crDatabase;
Tables crTables;
TableLogOnInfo crTableLogOnInfo;
crReportDocument = new ReportDocument();
crReportDocument.Load("c:\\reports\\Homes.rpt",CrystalDecisions.Shared.
OpenReportMethod.OpenReportByTempCopy);
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = "DSNName Or Server Name";
crConnectionInfo.DatabaseName = "HopeAndHome";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "password";
foreach (Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
//loop through all the report objects in there to find all subreports
foreach (ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
crSubreportObject = (SubreportObject)crReportObject;
//open the subreport object and logon as for the general report
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
crDatabase = crSubreportDocument.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
{
crTableLogOnInfo = aTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogOnInfo);
}
}
}
}