优化报表查看器以避免出现内存异常

时间:2014-08-20 17:12:15

标签: c# reporting-services reportviewer

我们已经使用C#和reportviewer实现了一个使用localreport.render方法创建PDF的流程。

当我们必须处理一个大文件,比如超过20000条记录时,该进程会因outofmemory异常错误而失败。我知道这个错误可能有很多原因,但我想知道的是,如果下面的代码是好的,或者你的专家可以给我一个更好的解决方案。

我有大约40个case语句,每个语句都调用不同的.rdlc文件,它基于类型(由于空间不足,我没有包含所有case语句)。

USP_Select_Batch_Address_To_Sort是在解决方案中定义的数据集,执行接受三个参数的存储过程(在本例中为批处理,运行和序列#)。

这种方法可行吗?你能提供什么其他解决方案?

public static void GenerateIndividualPDFs(string batch, string run, MainDataSet1 DS, MainDataSet1TableAdapters.USP_Select_Batch_Address_To_SortTableAdapter Ta, BindingSource bs, int intStart = 0, int intEnd = 0, string strLetterType = "")
        {
            reportDataSource.Name = "DataSet1";
            reportDataSource.Value = bs;
            int count;
            SqlDataAdapter adapter;

            var batchinfo = new clsBatchInfo();

            ReportViewer report = new ReportViewer();

            report.LocalReport.DataSources.Add(reportDataSource);


            if (!Directory.Exists(mstrFilePath + "\\" + batch + run))
            {
                DirectoryInfo di = Directory.CreateDirectory(mstrFilePath + "\\" + batch + run);
            }
            var DA = new clsGetBatchSort();

            //this dataAdapter will print the full set
            adapter = new SqlDataAdapter(DA.dsBatch_Sort(batch, run));


            DataSet ds = new DataSet();
            adapter.Fill(ds);
            count = ds.Tables[0].Rows.Count;

            int i = 0;
            int LetterTypeID = 0;

            report.ProcessingMode = ProcessingMode.Local;

  foreach (DataRow dr in ds.Tables[0].Rows)
            {
                i++;
                report.Clear();
                Ta.ClearBeforeFill = true;
                try
                {


                LetterTypeID = Convert.ToInt32(dr[2]);
                switch (LetterTypeID)
                {
                    case 1:                                      
                {

                    Ta.Fill(DS.USP_Select_Batch_Address_To_Sort, Convert.ToDecimal(batch), run,  Convert.ToInt32(dr["MPresortID"]));
                    report.LocalReport.ReportEmbeddedResource = "ReportsApplication1.ABC.MAENG.rdlc";
                    ExportReport(Convert.ToInt32(dr["MPresortID"]), batch, run, report);
                    continue;
                }
                    case 2:
                {
                     Ta.Fill(DS.USP_Select_Batch_Address_To_Sort, Convert.ToDecimal(batch), run,  Convert.ToInt32(dr["MPresortID"]));
                    report.LocalReport.ReportEmbeddedResource = "ReportsApplication1.ABC.MASPA.rdlc";
                    ExportReport(Convert.ToInt32(dr["MPresortID"]), batch, run, report);
                    continue;
                }
 catch (Exception ex)
                {
                    clsEmail.EmailMessage("Error Batch " + batch + run, "clsGenerateLetters " + ex.Message);
                    continue;
                    //MessageBox.Show(ex.Message);
                    //return "";
                }
            }
}

 private static void ExportReport(int PreSortID, string batch, string run, ReportViewer Report)
      {           

          Report.RefreshReport();

          Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string filenameExtension;

            string fmt = "00000000";



            byte[] bytes = Report.LocalReport.Render(
                "PDF", null, out mimeType, out encoding, out filenameExtension,
                out streamids, out warnings);

            string filename = mstrFilePath + "\\" + batch + run + "\\" + PreSortID.ToString(fmt) + ".PDF";
            using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }
            Report.LocalReport.ReleaseSandboxAppDomain();
      }

0 个答案:

没有答案