在不同环境中,Crystal Reports登录失败 [英] Crystal Reports Log On Failed in Different Environment
问题描述
我们有几种环境:
- 发展
- 天蓝色
- 在Prem
Crystal Report从SQL Server中提取数据,但是数据库因环境而异.
The Crystal Report pulls data from a SQL server, but the database is different depending on the environment.
项目中有一个名为 connectionStrings.config
的配置文件,我们用于网站的连接字符串存储在该文件中.该文件的内容如下所示:
There is a config file in the project called connectionStrings.config
and the connection string that we use for the website is stored there. The contents of the file looks like this:
<connectionStrings>
<add name="dataModel" connectionString="data source=...;initial catalog=...;user id=...;password=...;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
要加载报告,我设置了以下类别:
To load the report I setup the following class:
public class CrystalReportUtil
{
static string ReportsDirectory = ConfigurationManager.AppSettings["ReportsDirectory"];
private static string ConnectionString = ConfigurationManager.ConnectionStrings["dataModel"].ConnectionString;
public static ReportDocument GetReport(string reportName)
{
var report = new ReportDocument();
var fileDirectory = $"{Utilities.GetProjectRoot()}/Reports";
if (System.Web.HttpContext.Current != null)
{
fileDirectory = System.Web.HttpContext.Current.Server.MapPath(ReportsDirectory);
}
string file = Path.Combine(fileDirectory, $"{reportName}.rpt");
if (!File.Exists(file))
{
throw new System.Exception($"Unable to find report file: {file}");
}
report.Load(file);
var builder = new SqlConnectionStringBuilder(ConnectionString);
var dataSource = builder.DataSource;
var user = builder.UserID;
var password = builder.Password;
var database = builder.InitialCatalog;
RecursivelyRemapConnection(report, user, password, dataSource, database);
report.VerifyDatabase();
return report;
}
private static void RecursivelyRemapConnection(ReportDocument report, string username, string password, string server, string database)
{
foreach (IConnectionInfo connection in report.DataSourceConnections)
{
connection.SetLogon(username, password);
connection.SetConnection(server, database, false);
}
report.SetDatabaseLogon(username, password);
foreach (Table table in report.Database.Tables)
{
table.LogOnInfo.ConnectionInfo.ServerName = server;
table.LogOnInfo.ConnectionInfo.DatabaseName = database;
table.LogOnInfo.ConnectionInfo.UserID = username;
table.LogOnInfo.ConnectionInfo.Password = password;
table.LogOnInfo.ConnectionInfo.IntegratedSecurity = false;
}
if (!report.IsSubreport)
{
foreach (ReportDocument subreport in report.Subreports)
{
RecursivelyRemapConnection(subreport, username, password, server, database);
}
}
}
}
问题在于,在我的开发环境中,它工作得很好,而在其他环境中,我遇到以下异常:
The issue is that in my development environment, it is working just fine whereas in the other environments I get the following exception:
登录失败.没错.
Log on failed. No error.
在CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocumentClass.VerifyDatabase()
at CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocumentClass.VerifyDatabase()
在CrystalDecisions.ReportAppServer.ReportClientDocumentWrapper.VerifyDatabase()
at CrystalDecisions.ReportAppServer.ReportClientDocumentWrapper.VerifyDatabase()
在CrystalDecisions.CrystalReports.Engine.ReportDocument.VerifyDatabase()
at CrystalDecisions.CrystalReports.Engine.ReportDocument.VerifyDatabase()
我通过执行以下操作验证了连接在我的开发环境中正在更改:
I verified that the connection is changing in my development environment by doing the following:
- 创建我的数据源的副本
- 在设计器中,将数据源设置为复制的数据库
- 删除副本
- 在设计器中预览报告无法加载(按预期)
- 使用我的实用程序类生成报告
最后一步成功生成了报告,该报告告诉我数据源正在更改,否则我会期望登录失败.
The last step successfully generates the report which tells me that the datasource is changing, otherwise I would expect a logon failure.
之所以特别令人沮丧的是,一些非开发环境中的报告运行良好,而另一些则在产生异常.因此,我不明白为什么它适用于某些功能,但不适用于所有功能.
What makes this particularly frustrating is that some reports in the non-developing environments work fine whereas some are producing the exception. So I don't understand why it works for some, but not for all.
更新
如果我将开发环境中的连接字符串更改为Azure或本地部署中使用的连接字符串,则它仍可在我的开发环境中使用.我真的不相信问题出在我的连接字符串上,但是我还是不知道是什么原因导致登录失败.
If I change my connection string in my development environment to the connection string used in Azure or in the On-Prem, it still works in my development environment. I really don't believe that the issue is with my connection string, but then again I have no idea what is causing the logon failure at this point.
推荐答案
经过几天的研究,我终于找到了引起问题的原因.
After several days of looking into this, I finally found out what was causing the issue.
失败的特定报告使用的是 MSOLEDBSQL
提供程序,而不是 SQLOLEDB
.
The particular report that was failing was using the MSOLEDBSQL
provider instead of SQLOLEDB
.
要解决此问题,我:
- 在Visual Studio中打开Crystal报表
- 右键单击报告
- 数据库>设置数据源位置
- 扩展了当前数据源的属性
- 双击提供程序
- 将值从
MSOLEDBSQL
更改为SQLOLEDB
- 重新输入登录凭据(即使它们在运行时已更改)
- 保存了报告
- 构建解决方案
- 重新发布了项目
这篇关于在不同环境中,Crystal Reports登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!