How do I pass a SQL Query from C# to Report Server 2008 R2?
I need to generate a SQL statement from C# dynamically and have it display data in a report using Report Server.
I already declared a parameter and dataset for the report in Report Server. Now I am able to pass the parameter from C# but I also want to pass SQL Query from C# to Report Server.
Thanks in advance.
While this isn’t very secure you could pass the SQL statement to a text field parameter in the report. Then you could pass that statement as a parameter to a stored procedure in SQL Server (assuming you use that). Then you could use sp_executesql to execute the statement.
http://msdn.microsoft.com/en-us/library/ms188001.aspx
One issue you may have with this is that the Report Server is going to expect a standard set of columns in your dataset so you will want the statement you pass to have a standard set of columns. I don’t think that reports can handle dynamic fields without a lot of trickery.
One thing we’ve done to handle “dynamic” columns is have the statement return “field_1”, “field_2”, etc as the result set and then map your report to those generic columns.