水晶报表,为什么我提供了详细信息后仍然要求登录数据库?

Crystal reports, why is it asking for database login even after I provided the details?(水晶报表,为什么我提供了详细信息后仍然要求登录数据库?)

本文介绍了水晶报表,为什么我提供了详细信息后仍然要求登录数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am generating a report, but the problem is even though I've supplied credentials, when the form containing the CrystalReport opens up, it still asks me for them, and the worst part is, I don't enter any thing in there, and just click finish, and it loads the report. So, if there is no need for credentials (or whatever) why is it asking me?

这是代码

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

        var myConInfo = new CrystalDecisions.Shared.TableLogOnInfo();
        reportDocument1.Load(constr);
        myConInfo.ConnectionInfo.DatabaseName = "ProjectData.mdb";
        myConInfo.ConnectionInfo.ServerName = Application.StartupPath + @"DataProjectData.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;
    }

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

When the form loads, this screen pop ups

而且,当出现这种情况时,我什么都不输入!那就对了!我只需单击完成,它就会完美地加载报告!那么,如果它不需要任何东西,为什么它会要求我登录?

推荐答案

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

We have lots of problems when we came to connect a crystal report to a different database to the one it was designed / created against. We ended up creating the following function to set it up correctly. It recursively sets the connection on all report tables and sub reports.

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

Also I seem to remember we had problems if the report was designed with Integrated Windows Authentications, and run with a simple username and password. So we always made sure we designed the reports with a simple username and password connection to the database.

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

这篇关于水晶报表,为什么我提供了详细信息后仍然要求登录数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:水晶报表,为什么我提供了详细信息后仍然要求登录数据库?

基础教程推荐