提示登录的水晶子报表



当运行显示具有子报表的晶体报表的应用时,系统会提示用户输入登录信息。

下面是我用来将登录信息传递给报告的代码。此代码在显示窗体时执行。我有一个测试 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 
 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 aaTable in crTables)
                     {
                         crTableLogOnInfo = aaTable.LogOnInfo;
                         crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
                         aaTable.ApplyLogOnInfo(crTableLogOnInfo);
                     }
                 }
            }
         }
    crystalReportViewer1.ReportSource = crReportDocument;
    crystalReportViewer1.Refresh();

我希望在传入登录凭据时运行报告而不提示用户。

试试这个: 链接

法典:

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);
            }
        }
    }
}

最新更新