【问题标题】:How to get Crystal Report all queries如何获取水晶报表所有查询
【发布时间】:2017-04-04 13:19:28
【问题描述】:

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

如何通过 C# 批量更新所有查询?是否可以? 或者 如何找出需要更改的问题?

【问题讨论】:

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


    【解决方案1】:

    私有字符串 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;
    
        } 
    

    【讨论】:

      【解决方案2】:

      这不要求开发者拥有许可的 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,
      ...
      ...
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多