以编程方式在CommandTable中更改SQL查询

2020-09-04 18:44发布

点击此处---> 群内免费提供SAP练习系统(在群公告中)加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)大家好,是否可以通过编程方式更改...

         点击此处--->   EasySAP.com群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


大家好,
是否可以通过编程方式更改报表 CommandTable 中的SQL文本?

出于研究目的,我创建了一个非常简单的报表,其中只有一个< strong> CommandTable 对象,其中包含以下sql:

从双精度中选择1111 AS N


然后我正尝试通过以下方式将此SQL替换为" SELECT 2222 AS N FROM dual":

 CrystalDecisions.CrystalReports.Engine.ReportDocument doc =
     新的CrystalDecisions.CrystalReports.Engine.ReportDocument();
 doc.Load("С:\\ Temp \\ Test.rpt",CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);

 CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag newMainPropertyBag =
     新的CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();
 CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag newInnerPropertyBag =
     新的CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();
 foreach(CrystalDecisions.Shared.NameValuePair2 pr2
     在doc.Database.Tables [0] .LogOnInfo.ConnectionInfo.Attributes.Collection中)
 {
     如果(pr2.Name.ToString()==" QE_LogonProperties")
     {
         foreach(CrystalDecisions.Shared.NameValuePair2 SubPair2在
             (pr2.Value作为CrystalDecisions.Shared.DbConnectionAttributes).Collection)
             newInnerPropertyBag.Add(SubPair2.Name.ToString(),SubPair2.Value.ToString());
         newMainPropertyBag.Add(" QE_LogonProperties",newInnerPropertyBag);
     }
     其他
         newMainPropertyBag.Add(pr2.Name.ToString(),pr2Value.ToString());
 }

 CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo newConnectionInfo =
     新的CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo();
 newConnectionInfo.Attributes = newMainPropertyBag;
 newConnectionInfo.Kind =
     CrystalDecisions.ReportAppServer.DataDefModel.CrConnectionInfoKindEnum.crConnectionInfoKindCRQE;
 newConnectionInfo.UserName =" scott";
 newConnectionInfo.Password =" xxxxx";

 CrystalDecisions.ReportAppServer.DataDefModel.CommandTable NewCmdTable =
     新的CrystalDecisions.ReportAppServer.DataDefModel.CommandTable();
 CrystalDecisions.ReportAppServer.DataDefModel.CommandTable OldCmdTable
     = doc.ReportClientDocument.DatabaseController.Database.Tables [0]
         作为CrystalDecisions.ReportAppServer.DataDefModel.CommandTable;
 NewCmdTable.ConnectionInfo = newConnectionInfo;
 NewCmdTable.Name = OldCmdTable.Name;
 NewCmdTable.QualifiedName = OldCmdTable.QualifiedName;
 NewCmdTable.Alias = OldCmdTable.Alias;
 NewCmdTable.CommandText =" SELECT 2222 AS N FROM dual";

 doc.ReportClientDocument.DatabaseController.SetTableLocationEx(OldCmdTable,NewCmdTable);
 doc.SetDatabaseLogon(" scott"," xxxxx");
 doc.VerifyDatabase();
 doc.SaveAs("С:\\ Temp \\ DoneTest.rpt");
 doc.Close(); 

很遗憾,此更改后,存储的报告无法完全正常运行,

因此,一旦我应用了 SetTableLocationEx 方法, ApplyLogOnInfo ReplaceConnection 方法就什么都不做,也不会抛出任何异常。
< br>当我查看此报告的数据库连接参数时,
我发现"数据库Dll"参数从" crdb_oracle.dll"更改为" crdb_quary.dll"。

例如,在更改sql之前:

之后 SetTableLocationEx

为什么此数据库连接已更改?

有没有一种正确的方法来更改CommandTable中的SQL而无需更改数据库?

(1.9 kB)
3条回答
昵称总是被占用
2020-09-04 19:20

感谢戴尔的评论,我尝试通过ReportClientDocument对象模型工作

在某些情况下,ISCDClientDocument.Open(object,int)可能无济于事,因为我从本地磁盘打开了测试报告。

其他信息。

我接受了官方的" NET-CS2005_RAS-Unmanaged_CR115_Modify_Command-Table-SQL.zip "示例,并将其用于相同的更改。
SAP示例中的以下代码 是在我的测试中使用的:

私有无效CRChangeSQLStatement()
 {
    //Crystal Reports声明
     CrystalDecisions.CrystalReports.Engine.ReportDocument boReportDocument =
         新的CrystalDecisions.CrystalReports.Engine.ReportDocument();
     CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument boReportClientDocument;
     CrystalDecisions.ReportAppServer.Controllers.DataDefController boDataDefController;
     CrystalDecisions.ReportAppServer.Controllers.DatabaseController boDatabaseController;
     CrystalDecisions.ReportAppServer.DataDefModel.Database boDatabase;
     CrystalDecisions.ReportAppServer.DataDefModel.CommandTable boCommandTable;
     CrystalDecisions.ReportAppServer.DataDefModel.CommandTable boNewCommandTable;

    //使用CR .NET SDK加载报告并获取ReportClientDocument的句柄
     boReportDocument.Load(" C:\\ Temp \\ TestBefore.rpt");

    //使用CR .NET SDK加载报告并获取ReportClientDocument的句柄
     boReportClientDocument = boReportDocument.ReportClientDocument;

    //使用DataDefController访问数据库和命令表。
     boDataDefController = boReportClientDocument.DataDefController;
     boDatabase = boDataDefController.Database;

    //要更改命令表的文本,必须克隆原始命令表。
     boCommandTable =(CrystalDecisions.ReportAppServer.DataDefModel.CommandTable)boDatabase.Tables [0];
     boNewCommandTable =(CrystalDecisions.ReportAppServer.DataDefModel.CommandTable)boCommandTable.Clone(true);
     boNewCommandTable.CommandText =" SELECT 1111 AS N FROM dual";

    //使用DatabaseController设置新表的位置
     boDatabaseController = boReportClientDocument.DatabaseController;
     boDatabaseController.SetTableLocation(boCommandTable,boNewCommandTable);

    //保存更改的报告
     boReportDocument.SaveAs(" C:\\ Temp \\ sql_changed_report.rpt");
     boReportDocument.Close();
     boReportDocument.Dispose();
 } 

因此,在那之后,我得到了" sql_changed_report.rpt"文件,然后尝试通过以下方式更改唯一表[0]中的LogOnInfo:

私有无效ChangeLogOnInfo()
 {
     CrystalDecisions.CrystalReports.Engine.ReportDocument doc =
         新的CrystalDecisions.CrystalReports.Engine.ReportDocument();
     doc.Load(" C:\\ Temp \\ sql_changed_report.rpt");
     CrystalDecisions.Shared.TableLogOnInfo loginf =
         doc.Database.Tables [0] .LogOnInfo.Clone()as CrystalDecisions.Shared.TableLogOnInfo;
     loginf.ConnectionInfo.ServerName =" localhostname_new";
     loginf.ConnectionInfo.DatabaseName =" db01_new";
     loginf.ConnectionInfo.UserID =" scott_new";
     loginf.ConnectionInfo.Password =" xxxx_new";

    /* 警告! 下一个方法ApplyLogOnInfo什么都不做,
      *,并且在此报告中不起作用
      *因为以前完成了SetTableLocation */
     doc.Database.Tables [0] .ApplyLogOnInfo(loginf);

     doc.SaveAs(" C:\\ Temp \\ logofinf_changed_report.rpt");
     doc.Close();
     doc.Dispose();
 } 

不幸的是,此报告具有与我在上面的第一篇文章中相同的问题,如下所示:

  • 此存储的报表的ApplyLogOnInfo和ReplaceConnection方法不执行任何操作,也不引发任何异常
  • "数据库Dll"参数显示为" crdb_quary.dll",而不是所需的" crdb_oracle.dll"

请您澄清一下,在执行SetTableLocation方法后,这些必需的方法ApplyLogOnInfo和ReplaceConnection不能正常工作吗?

您能否提供一个示例,说明如何以编程方式在CommandTable中更改SQL,然后获得工作报告?

谢谢,
Artem

一周热门 更多>