CrystalReportsViewer提示输入密码,但不接受

时间:2019-08-30 20:21:25

标签: crystal-reports

我正在尝试使用CrystalReportViewer控件在Visual Studio 2012项目中显示报表。我提供了连接到数据库所需的所有信息,包括服务器名称,端口号,数据库名称,用户名和密码。当我运行下面的程序时,控件将显示一个对话框,其中包含正确的连接字符串,数据库名称和用户,并提示我输入密码,即使我已经给了密码。当我输入正确的密码后,会出现一个弹出窗口,提示“登录失败。请重试。”我在做什么错了?

顺便说一句,此报告中没有子报告。当我在下面运行该程序时,我在处理子报表的代码中放置了一个断点,但从未被击中。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;

namespace Example
{
    public partial class Form1 : Form
    {
        private string _serverName;
        private string _databaseName;
        private string _user;
        private string _passwd;
        private ReportDocument _report;

        public Form1()
        {
            InitializeComponent();
        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
            string reportFile = "C:\\Users\\rdrichardson\\OneDrive - Rad-Con\\CAPS Builds\\trunk\\Debug Crawfordsville\\Reports\\English\\allbases_no_subreports.rpt";
            _report = new ReportDocument();            
            _report.Load(reportFile);

            _serverName = "Provider=Provider=PostgreSQL OLE DB Provider;Data Source=localhost;Port=5432timeout=1000;";
            // _serverName = "Driver={PostgreSQL};Server=IP address;Port=5432;Database=Algoma;Uid=caps;Pwd=asdlkjqp;";
            // _serverName = "Driver={PostgreSQL};Server=IP address;Port=5432;";
            _databaseName = "Algoma";
            _user = "caps";
            _passwd = "asdlkjqp";

            LogOnToTables();

            crystalReportViewer1.ReportSource = _report;
        }
        private void LogOnToTables()
        {
            string location;
            ReportObjects crReportObjects;
            SubreportObject crSubreportObject;
            ReportDocument crSubreportDocument;
            Database crDatabase;
            Tables crTables;
            TableLogOnInfo crTableLogOnInfo;

            ConnectionInfo crConnectInfo = new ConnectionInfo();

            if (_report == null) return;


            // Setup the ODBC/ADO connect string
            crConnectInfo.DatabaseName = _databaseName;
            crConnectInfo.ServerName = _serverName;
            crConnectInfo.Type = ConnectionInfoType.CRQE;
            crConnectInfo.UserID = _user;
            crConnectInfo.Password = _passwd;

            _report.SetDatabaseLogon(_user, _passwd);
            _report.DataSourceConnections.Clear();

            crDatabase = _report.Database;
            crTables = crDatabase.Tables;

            //loop through all the tables and pass in the connection info
            foreach (Table crTable in crTables)
            {
                location = crTable.Location;
                crTableLogOnInfo = crTable.LogOnInfo;
                crTableLogOnInfo.ConnectionInfo = crConnectInfo;
                // crTable.LogOnInfo.ConnectionInfo.DatabaseName = string.Empty;
                crTable.ApplyLogOnInfo(crTableLogOnInfo);
            }

            //set the crSections object to the current report's sections
            Sections crSections = _report.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 to find all the subreports
                foreach (CrystalDecisions.CrystalReports.Engine.ReportObject crReportObject in crReportObjects)
                {
                    if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                    {
                        //you will need to typecast the reportobject to a subreport 
                        //object once you find it
                        crSubreportObject = (SubreportObject)crReportObject;

                        //open the subreport object
                        crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);

                        //set the database and tables objects to work with the subreport
                        crDatabase = crSubreportDocument.Database;
                        crTables = crDatabase.Tables;

                        //loop through all the tables in the subreport and 
                        //set up the connection info and apply it to the tables
                        foreach (Table crTable in crTables)
                        {
                            location = crTable.Location;
                            crTableLogOnInfo = crTable.LogOnInfo;
                            crTableLogOnInfo.ConnectionInfo = crConnectInfo;
                            crTable.ApplyLogOnInfo(crTableLogOnInfo);
                        }
                    }
                }
            }
        }
    }


}

1 个答案:

答案 0 :(得分:0)

_user = "caps";
_passwd = "asdlkjqp";

此值是硬编码的,无论您输入什么,它将始终使用此密码。尝试提供实际的用户名/密码,而不是asdlkjqp