How can I run a . rdl (Reporting Service) file with Report Viewer without using the server?

Asked

Viewed 876 times

1

Good Afternoon, I am in need of a solution to run the . rdl local files without using the Report Services server, I need to use the local file. The goal is to use existing reports, with SQL Server and Report Services, in another database. Anyone have any suggestions? Thank you.

1 answer

1

My database is Mysql, so my reports are RDLC. Both use the same xml, but the RDLC allows not having the connection nor the query. This way I make the query and connection to the bank via C# and send to the RDLC as a Datasource that issues it takes charge of generating and issuing the report.

It is possible to convert the RDL to RDLC: https://msdn.microsoft.com/en-us/library/ms252109(v=vs.80). aspx

This done, create a basic function to send your reports directly in Responsestream, type this:

private void Renderizar(string nomeReport, Formato formato, Dictionary<string, object> dataSources, Dictionary<string, object> parameters = null)
    {
        var deviceInfo = string.Format("<DeviceInfo>" + "  <OutputFormat>{0}</OutputFormat>" + "</DeviceInfo>", "PDF"|"Excel");

        var report = new LocalReport();
        using (IO.StreamReader arquivo = new IO.StreamReader(Server.MapPath(string.Format("~/Relatorios/{0}.rdlc", nomeReport)))) {
            var strre = arquivo.ReadToEnd();
            report.Refresh();
            report.LoadReportDefinition(new IO.StringReader(strre));
        }

        foreach (var item in dataSources) {
            var DataSource = new ReportDataSource(item.Key, item.Value);
            report.DataSources.Add(DataSource);
        }

        List<ReportParameter> @params = new List<ReportParameter>();
        if (parameters != null) {
            @params = new List<ReportParameter>();
            foreach (var item in parameters) {
                @params.Add(new ReportParameter(item.Key, item.Value.ToString()));
            }
        }

        string mimeType = "";
        string ext = "";
        string encoding = "";

        Warning[] warnings = null;
        string[] streams = null;

        if (@params != null) {
            report.SetParameters(@params);
        }

        var bytes = report.Render(formato.ToString(), deviceInfo, out mimeType, out encoding, out ext, out streams, out warnings);

        Response.ContentType = mimeType;
        Response.BinaryWrite(bytes);


    }

There to call I use:

var dataSources = new Dictionary<string, object>();
dataSources.Add("DataSourceNameDENTRO_DO_REPORT", List<Objetos>);

var parametros = new Dictionary<string, object>();
parametros.Add("Data", "VALOR PARA EXIBIR COMO UM PARAMETRO NO REPORT");

Renderizar("NomeRelatorio", "PDF", dataSources, parametros);
  • Great solution, now just a doubt, how would you call a rdlc that has subreport with parameter? Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.