Crystal reports, why is it asking for database login even after I provided the details?

后端 未结 4 1919
天涯浪人
天涯浪人 2020-12-18 08:36

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

4条回答
  •  -上瘾入骨i
    2020-12-18 08:53

    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.

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

提交回复
热议问题