如何获取Crystal报告所有查询

时间:2017-04-04 13:19:29

标签: c# sql-server database crystal-reports multi-database

我的项目中有24个数据库。一个数据库已分离到另外3个数据库。现在,我必须更改Crystal Report的所有查询。

如何通过C#批量更新所有查询?可能吗? 要么 我怎样才能找出需要改变的问题?

2 个答案:

答案 0 :(得分:0)

私有字符串getSQL(InfoObject iObject,EnterpriseSession eSession)         {             CrystalDecisions.CrystalReports.Engine.ReportDocument boReportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();             CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument boReportClientDocument;             CrystalDecisions.ReportAppServer.Controllers.DataDefController boDataDefController;             CrystalDecisions.ReportAppServer.DataDefModel.Database boDatabase;             CrystalDecisions.ReportAppServer.DataDefModel.CommandTable boCommandTable;

        // Load the report using the CR .NET SDK and get a handle on the ReportClientDocument
        boReportDocument.Load(iObject,eSession);
        boReportClientDocument = boReportDocument.ReportClientDocument;

        // Use the DataDefController to access the database and the command table.
        // Then display the current command table SQL in the textbox.
        boDataDefController = boReportClientDocument.DataDefController;
        boDatabase = boDataDefController.Database;

        string sql;
        sql = "";

        for (int i = 0; i < boDatabase.Tables.Count; i++)
        {
            CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable tableObject = boDatabase.Tables[i];

            if (tableObject.ClassName == "CrystalReports.Table")
            {
                sql = sql + "Table " + i + ": " + tableObject.Name;
            }
            else
            {
                boCommandTable = (CrystalDecisions.ReportAppServer.DataDefModel.CommandTable)boDatabase.Tables[i];
                sql = sql + "Query " + i + ": " + boCommandTable.CommandText;
            }
            sql += Environment.NewLine;

        }

        foreach (string subName in boReportClientDocument.SubreportController.GetSubreportNames())
        {
            CrystalDecisions.ReportAppServer.Controllers.SubreportClientDocument subRCD = boReportClientDocument.SubreportController.GetSubreport(subName);

            for (int i = 0; i < boDatabase.Tables.Count; i++)
            {
                CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable tableObject = boDatabase.Tables[i];

                if (tableObject.ClassName == "CrystalReports.Table")
                {
                    sql = sql + "Table " + i + ": " + tableObject.Name;
                }
                else
                {
                    boCommandTable = (CrystalDecisions.ReportAppServer.DataDefModel.CommandTable)subRCD.DatabaseController.Database.Tables[i];
                    sql = sql + "Subreport " + subName + " - Query " + i + ": " + boCommandTable.CommandText;
                }
                sql += Environment.NewLine;
            }

        }


        // Clean up
        return sql;

    } 

答案 1 :(得分:0)

这不是要求开发人员具有许可的RAS SDK吗?

无论如何,只要使用我的CR开发者许可证,该软件都可以在AFAICT上使用,非常好。 我确实对其做了一些修改以使用StringBuilder等,如下所示... 也是

  • 使用log4net转储查询...
  • 显示RPT的加载位置 来自(rptSourceName)
  • 呈现RPT SummaryInfo对象中的数据

      private void DumpQueries(CrystalDecisions.CrystalReports.Engine.ReportDocument doc, string rptSourceName)
    {
        try
        {
            //CrystalDecisions.CrystalReports.Engine.ReportDocument boReportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    
            CommandTable boCommandTable;
            var boReportClientDocument = doc.ReportClientDocument;
            var boDataDefController = boReportClientDocument.DataDefController;
            var boDatabase = boDataDefController.Database;
    
            var reportSummary = doc.SummaryInfo;
            var reportName = doc.Name ?? doc.FileName;
    
            //return; // disabled for now.
    
            //foreach (dynamic table in doc.ReportClientDocument.DatabaseController.Database.Tables)
            //{
            //    if (table.ClassName == "CrystalReports.CommandTable")
            //    {
            //        string commandSql = table.CommandText;
    
            //        Log?.DebugFormat(@"Report object: {0} - SQL: {1}", doc.Name, commandSql);
            //    }
            //}
    
            // revised based on  https://stackoverflow.com/questions/43208449/how-to-get-crystal-report-all-queries/57822774#57822774
    
    
    
            var sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendLine("---------- Summary -----------");
            sb.AppendLine($@"Report source: {rptSourceName}");
            sb.AppendLine($"Title: {reportSummary.ReportTitle}");
            sb.AppendLine($"Subject: {reportSummary.ReportSubject}");
            sb.AppendLine($"Comments: {reportSummary.ReportComments}");
            sb.AppendLine($"Author: {reportSummary.ReportAuthor}");
            sb.AppendLine($"Keywords: {reportSummary.KeywordsInReport}");
            sb.AppendLine($"Last Saved By: {reportSummary.LastSavedBy}");
            sb.AppendLine($"Revision #: {reportSummary.RevisionNumber}");
            sb.AppendLine("------------------------------");
    
    
    
            for (var i = 0; i < boDatabase.Tables.Count; i++)
            {
                ISCRTable tableObject = boDatabase.Tables[i];
    
                if (tableObject.ClassName == "CrystalReports.Table")
                {
                    sb.AppendLine(@"Table " + i + ": " + tableObject.Name);
    
                }
                else
                {
                    boCommandTable = (CrystalDecisions.ReportAppServer.DataDefModel.CommandTable)boDatabase.Tables[i];
                    sb.AppendLine(@"Query " + i + ": " + boCommandTable.CommandText);
                }
    
    
            }
    
            sb.AppendLine("------------------------------");
            sb.AppendLine("");
    
            sb.AppendLine("--------- Subreports ---------");
    
            foreach (string subName in boReportClientDocument.SubreportController.GetSubreportNames())
            {
    
    
    
                SubreportClientDocument subRcd = boReportClientDocument.SubreportController.GetSubreport(subName);
                sb.AppendLine($@"Subreport object: {subRcd.Name}");
                sb.AppendLine("------------------------------");
                for (var i = 0; i < boDatabase.Tables.Count; i++)
                {
                    CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable tableObject = boDatabase.Tables[i];
    
                    if (tableObject.ClassName == "CrystalReports.Table")
                    {
                        sb.AppendLine(@"Table " + i + ": " + tableObject.Name);
                    }
                    else
                    {
                        boCommandTable = (CrystalDecisions.ReportAppServer.DataDefModel.CommandTable)subRcd.DatabaseController.Database.Tables[i];
                        sb.AppendLine(@"Query " + i + ": " + boCommandTable.CommandText);
                    }
                    //sql += Environment.NewLine;
                }
    
    
            }
    
            Log?.Debug($@"Report contents: {  sb.ToString()}");
    
        }
        catch (Exception ex)
        {
            Log?.Error(ex);
            throw;
        }
    }
    

生成:

    Report contents: 
---------- Summary -----------
Report source: zzzzzzzzz
Title: zzzzzzzz
Subject: zzzzzzzzz
Comments: zzzzzzzzz
Author: XXXXXXX
Keywords: XXXXXXX
Last Saved By:XXXXXXX
Revision #: 411
------------------------------
Query 0: SELECT B1.Brkey,I1.inspkey,
   B1.Strucname,
   P2.Pon_Session_Batch_Key,
   B1.Bridge_Id,
   B1.Struct_Num,
...
...
...