Search code examples
c#sqlcrystal-reports

Return all possible index parameters


I am working with C# and Crystal Reports library. I have a method that gets the SQL query for a specified parameters as shown below;

pi.GetIndexParameters()))[0].CommandText;

My issue is that once a report has more than one query - how can I find the number of indexes (queries) the report has and return each SQL query then?

//Returns SQL query based on index parameter
public string readSqlQuery(ReportDocument rd)
     {
         if (!rd.IsLoaded)
             throw new ArgumentException("Please ensure that the reportDocument has been loaded");
         PropertyInfo pi = rd.Database.Tables.GetType().GetProperty("RasTables", BindingFlags.NonPublic | BindingFlags.Instance);

         return ((dynamic)pi.GetValue(rd.Database.Tables, pi.GetIndexParameters()))[0].CommandText;
     }

If you need any further information, let me know.


Solution

  • I would do it like this:

    1. Get and save the returned IndexParameters in a list // UNNECESSARY, you can skip this
    2. loop through the list and get every value/CommandText, stored in a new list

    3. either concatenate the list into a single string and return it OR

    4. return the list of strings

    If you're gonna go for 3, i assume you'd want the results from all the queries together? Then make sure you make a UNION ALL on your queries... Example:

    SELECT t.id, t.name, t.date FROM table t WHERE <some condition> UNION ALL SELECT t.id, t.name, t.date FROM table t WHERE <some other condition> UNION ALL SELECT t.id, t.name, t.date FROM table t WHERE <some third condition>

    So you're basically concatenating the queries with a "UNION ALL" and get a single result.