水晶报表中的 SQL 查询不会更新

本文关键字:更新 查询 SQL 报表 水晶 | 更新日期: 2023-09-27 18:32:47

我正在编写一个应用程序,用于更改报告文件中的Crystal Reports数据库访问参数。我在 .NET Windows 窗体应用程序中打开报表,并应用 SDK 功能来更改驱动程序类型 (ODBC/OLEDB(、服务器名称、数据库名称、用户、密码、身份验证类型等。我的数据库名称有问题。我的代码确实更改了表 ConnectionInfo 的特定属性(在子报表中也是如此(,但无法更新报表中的常规 SQL 查询。这会导致报表仍访问旧数据库。

因此,如果原始报表配置为访问database_1并且我将其更改为 database_2,则所有表属性将正确更改为 database_2(可在设计器中验证(。不过,它仍然会在查询中database_1。数据库名称在 SDK RowsetController.GetSQLStatement(( 结果和 Crystal Reports Developer 查询视图(Database->Show SQL Query...(中保持不变。

此外,在转换发生时,我必须使两个数据库(database_1和database_2(都在线,否则我会在 GetSQLStatement(当database_1离线时;因为它仍然引用它(或 SetTableLocation(当database_2离线时 - 这是预期和可接受的行为(。如果两个数据库都处于联机状态,则没有错误。

这正是我正在使用的:

1( CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(filePath, OpenReportMethod.OpenReportByTempCopy((...)

2(制作和填充CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag

3( 遍历 CrystalDecisions.ReportAppServer.DataDefModel.Tables,并为每个属性应用 SetTableLocaiton(( 的所有属性。

4( 对每个子报表重复此操作

5( RowsetController.GetSQLStatement(( 查看报表的 sql 查询。

有没有办法根据新表ConnectionInfos(似乎设置正确(更新查询?我什至看不到手动更新查询(GET,搜索和替换,SET(的任何可能性。

我正在使用:

.NET 4.5,Visual Studio 2012,CR 用于 VS 13.0.5,水晶报告开发人员 9.2.2.693 用于结果验证(源报告也使用它创建(

水晶报表中的 SQL 查询不会更新

答:为每个表设置属性限定名称。限定名称是表的全名,包括 DbName。这稍后会显示在报表的 SQL 查询中。通过限定名称,我们理解:

myDatabase.mySchema.myTableName

代码示例:

CrystalDecisions.ReportAppServer.DataDefModel.Table boTable = new CrystalDecisions.ReportAppServer.DataDefModel.Table();
CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag boMainPropertyBag = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();
CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag boInnerPropertyBag = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();
// Custom function to fill property bags with values which influence the table properties as seen in CR Developer
FillPropertyBags(boMainPropertyBag, boInnerPropertyBag);
CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo boConnectionInfo = new CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo();
boConnectionInfo.Attributes = boMainPropertyBag;
boConnectionInfo.Kind = CrystalDecisions.ReportAppServer.DataDefModel.CrConnectionInfoKindEnum.crConnectionInfoKindCRQE;
boTable.ConnectionInfo = boConnectionInfo;
CrystalDecisions.ReportAppServer.DataDefModel.Tables boTables = boReportDocument.ReportClientDocument.DatabaseController.Database.Tables;
for (int i = 0; i < boTables.Count; i++)
{
   boTable.Name = boTables[i].Name;
   // the QualifiedName is directly taken into the CR general query so this is a quick fix to change it
   boTable.QualifiedName = boTables[i].QualifiedName.Replace("oldDbName", "newDbName"); 
   boTable.Alias = boTables[i].Alias;
   boReportDocument.ReportClientDocument.DatabaseController.SetTableLocation(boTables[i], boTable);
}

呃......研究了一整天,并在SO上发布问题后找到了答案。