How can I retrieve the SQL SELECT statement used in an Crystal Report?

前端 未结 2 557
别跟我提以往
别跟我提以往 2021-02-06 09:19

I am currently working on a program in C# that allows our users to run, view and export a batch of Crystal Reports. The reports were made using the Crystal Reports 2008 GUI. One

相关标签:
2条回答
  • 2021-02-06 09:39

    Alright, so dotjoe gave me all of the hints that I needed to work this out. The following code can be used to pull the command text from a crystal report.

    public string getCommandText(ReportDocument rd)
    {
        if (!rd.IsLoaded)
            throw new ArgumentException("Please ensure that the reportDocument has been loaded before being passed to getCommandText");
        PropertyInfo pi = rd.Database.Tables.GetType().GetProperty("RasTables", BindingFlags.NonPublic | BindingFlags.Instance);
        return ((dynamic)pi.GetValue(rd.Database.Tables, pi.GetIndexParameters()))[0].CommandText;
    }
    

    It looks a bit messy, but it makes some sort of sense when you start wading through it. Basically, it uses reflection to get the value of the CommandText property, with a little dynamics thrown in to get past the dynamic properties in the ReportDocument.

    This is pulling the command text for me, but I have had no time to do any tests on the code. I'm sure I'll be making some tweaks once I've had the time to work with this. I have no clue what happens with reports that do not use "SQL Commands". I'll post a comment once I've tested this further.

    • Scott

    P.S. This requires that you reference the standard reflection/dynamic libraries, as well as the following Crystal Report libraries:

    crystaldecisions.reportappserver.datadefmodel

    crystaldecisions.crystalreports.engine

    crystaldecisions.shared

    0 讨论(0)
  • 2021-02-06 09:41

    I realize this is a very old question, but thought I would offer an alternative for those who stumble across this but need a framework target of 3.5 (dynamic is not available in 3.5).

    You will need the following references for this solution to work.

    using CrystalDecisions.ReportAppServer.DataDefModel;
    using CrystalDecisions.CrystalReports.Engine;
    

    Then just access the ClientDoc interface with the following and return a list of Command Text strings.

        private static List<string> GetCommandText(CrystalDecisions.CrystalReports.Engine.ReportDocument report)
        {
            var rptClientDoc = report.ReportClientDocument;
            return rptClientDoc.DatabaseController.Database.Tables.OfType<CommandTable>()
                  .Select(cmdTbl => cmdTbl.CommandText).ToList();
        }
    
    0 讨论(0)
提交回复
热议问题