Crystal reports,为什么我提供了详细信息后还要求登录数据库?

问题描述 投票:0回答:6

我正在生成一份报告,但问题是即使我提供了凭据,当包含 CrystalReport 的表单打开时,它仍然要求我提供这些凭据,最糟糕的是,我没有在其中输入任何内容,然后只需单击“完成”即可加载报告。那么,如果不需要凭据(或其他什么),为什么它要问我?

这是代码

    private void MainReport_Load(object sender, EventArgs e)
    {
        var constr = string.Empty;
        constr = Application.StartupPath;
        if (Generate.bForProjects)
            constr = Path.Combine(constr, @"Reports\Projects.rpt");
        else
            constr = Path.Combine(constr, @"Reports\Other.rpt");

        var myConInfo = new CrystalDecisions.Shared.TableLogOnInfo();
        reportDocument1.Load(constr);
        myConInfo.ConnectionInfo.DatabaseName = "ProjectData.mdb";
        myConInfo.ConnectionInfo.ServerName = Application.StartupPath + @"\Data\ProjectData.mdb";
        myConInfo.ConnectionInfo.Password = "";
        myConInfo.ConnectionInfo.UserID = "";
        reportDocument1.Database.Tables[0].ApplyLogOnInfo(myConInfo);

        reportDocument1.Refresh();

        crystalReportViewer1.ReportSource = reportDocument1;
        crystalReportViewer1.Width = this.Width - 50;
        crystalReportViewer1.Height = this.Height - 100;
    }

当表单加载时,会弹出此屏幕

enter image description here

而且,当出现这种情况时,我不会输入任何内容!这是正确的!我只需单击完成即可完美加载报告!所以,如果它不需要任何东西,为什么它要求我登录?

c# winforms ms-access crystal-reports
6个回答
3
投票

仅当您的数据集或数据表为空时才会出现。所以请确保它有数据。


2
投票

当我们将水晶报表连接到与其设计/创建的数据库不同的数据库时,我们遇到了很多问题。我们最终创建了以下函数来正确设置它。它递归地设置所有报表和子报表上的连接。

我似乎还记得,如果报告是使用集成 Windows 身份验证设计的,并使用简单的用户名和密码运行,我们会遇到问题。因此,我们始终确保使用简单的用户名和密码连接到数据库来设计报告。

private static void SetConnection(ReportDocument report, string databaseName, string serverName, string userName, string password)
{
    foreach (Table table in report.Database.Tables)
    {
        if (table.Name != "Command")
        {
            SetTableConnectionInfo(table, databaseName, serverName, userName, password);
        }
    }

    foreach (ReportObject obj in report.ReportDefinition.ReportObjects)
    {
        if (obj.Kind != ReportObjectKind.SubreportObject)
        {
            return;
        }

        var subReport = (SubreportObject)obj;
        var subReportDocument = report.OpenSubreport(subReport.SubreportName);
        SetConnection(subReportDocument, databaseName, serverName, userName,  password);
    }
}

private static void SetTableConnectionInfo(Table table, string databaseName, string serverName, string userName, string password)
{
    // Get the ConnectionInfo Object.
    var logOnInfo = table.LogOnInfo;
    var connectionInfo = logOnInfo.ConnectionInfo;

    // Set the Connection parameters.
    connectionInfo.DatabaseName = databaseName;
    connectionInfo.ServerName = serverName;
    connectionInfo.Password = password;
    connectionInfo.UserID = userName;
    table.ApplyLogOnInfo(logOnInfo);

    if (!table.TestConnectivity())
    {
        throw new ApplicationException(Resource.UnableToConnectCrystalReportToDatabase);
    }

    table.Location = Database + "." + "dbo" + "." + table.Location;
}

1
投票

只需创建报告对象并添加以下链接 rptobj 是 crystalreport 对象,setdatabaselogin 是以用户 ID 和密码作为参数的方法。

rptobj.setdatabaselogon(userid,password)


1
投票

包括 --> 设置数据库登录

效果很好 - 添加了 1 行 [SetDatabaseLogon],现在一切正常!

注意:连接到 MariaDB 10.3

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

cryRpt = new ReportDocument();
        string the_report = comboBox1.Text.ToString();
        //cryRpt.Load("D:\\CrystalReports\\feedback.rpt");
        cryRpt.Load(the_report);

        cryRpt.SetDatabaseLogon("xyz", "123");

        crystalReportViewer1.ReportSource = cryRpt;
        // set up crystal parameters
        //crystalReportViewer1.DisplayToolbar = false; //removes the toolbar
        crystalReportViewer1.ToolPanelView = 0;  //removes the Group Tree on the Left
        crystalReportViewer1.ShowLogo = false;
        // end set up crystal parameters
        //crystalReportViewer.Zoom(50); crystalReportViewer.RefreshReport()
        crystalReportViewer1.Zoom(1);   
        crystalReportViewer1.Refresh();

        //generate the filename
        string dt_stamp = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
        //parse the report filename from combobox drop down
        string lastFragment = comboBox1.Text.Split('\\').Last();

        string create_file = "E:\\CrystalReportsSent\\"+ lastFragment.Replace(".rpt","") +"--" + dt_stamp.ToString() + ".pdf";
        //MessageBox.Show(create_file.ToString());
        pdfFile = create_file;

0
投票

请参阅我的回答此处,了解与报告文件一起保存的旧版 SQL Native Client 以及禁用的不安全加密协议(例如 TLS 1.0),这些协议会导致出现登录失败。


0
投票

使用此代码会有帮助。

internal static TableLogOnInfos GetLogOnInfos()
    {
        string server = DatabaseHelpers.DbHost;
        string database = DatabaseHelpers.DbName;
        string userID = DatabaseHelpers.DbUser;
        string password = DatabaseHelpers.DbUserPass;
        string table = DatabaseHelpers.DbTableName;
        TableLogOnInfo logOnInfo = new TableLogOnInfo();

        logOnInfo.ConnectionInfo.ServerName = server;
        logOnInfo.ConnectionInfo.DatabaseName = database;
        logOnInfo.ConnectionInfo.UserID = userID;
        logOnInfo.ConnectionInfo.Password = password;
        logOnInfo.TableName = table;

        return new TableLogOnInfos{logOnInfo};
    }

然后在页面加载时,您可以使用它。

 private void crystalReportViewer1_Load(object sender, EventArgs e)

{ reportTopCustomersRpt1.SetDatabaseLogon(DatabaseHelpers.DbUserPass,DatabaseHelpers.DbUserPass)。 crystalReportViewer1.LogOnInfo = CrystalReportManager.GetLogOnInfos(); }

© www.soinside.com 2019 - 2024. All rights reserved.