首页 文章

Crystal Reports中的SQL查询不会更新

提问于
浏览
0

我正在编写一个应用程序,它可以在报告文件中更改 Crystal Reports 数据库访问参数 . 我打开报告 .NET windows forms app 并应用SDK功能来更改驱动程序类型(ODBC / OLEDB),服务器名称,数据库名称,用户,密码,身份验证类型等 . 我有一个 problem with the database name . 我的代码可以更改表ConnectionInfo的特定属性(在子报表中),但在报表中更改 fails to update the general SQL Query . 这导致报告仍然访问旧数据库 .

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

转换发生时也是 I have to have both databases (database_1 and database_2) online ,否则我会在GetSQLStatement上获得异常(当database_1离线时;因为它仍然引用它)或SetTableLocation(当database_2离线时 - 这是预期和可接受的行为) . 如果两个db都在线,则没有错误 .

Here is exactly what I'm using:

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 for VS 13.0.5,Crystal Reports Developer 9.2.2.693,用于结果验证(也使用它创建源报告)

1 回答

  • 1

    答案:为每个表设置propper QualifiedName . QualifiedName是表的全名,包括DbName . 稍后会出现在报告的SQL查询中 . 通过合格的名称我们了解:

    myDatabase.mySchema.myTableName

    Code example:

    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上发布问题后找到答案 .

相关问题