65.9K
CodeProject 正在变化。 阅读更多。
Home

报表发布器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.27/5 (4投票s)

2013年1月2日

CPOL

1分钟阅读

viewsIcon

31635

downloadIcon

445

一个用于将 Microsoft 报告发布到 Microsoft WebService 的工具。

引言

Microsoft Web 服务为我们提供了上传报告(*.rdl 文件)以及报告所需的数据集和数据存储的功能。但是,当报告(*.rdl 文件)和数据集(*.rsd)数量较多时,上传所有文件、将报告文件链接到数据集,然后将所有数据集链接到数据源将需要花费时间。因此,此报告发布者工具将创建一个 Windows 界面,只需单击按钮,就可以以简单的方式完成上述活动。

背景

在继续之前需要的基础知识

  • Web 服务
  • Microsoft 的 Reporting Services 和函数
  • 报告、数据集和数据源
  • Visual Studio 中的 Windows 应用程序

使用代码 

使用此代码,我们可以以更快的方式将报告上传到 Microsoft 的 Reporting Services。 此应用程序使用的类如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web.Services.Protocols;
using Microsoft.SqlServer.ReportingServices2010;

创建此工具的步骤

  1. 在 Microsoft Visual Studio 中创建一个 Windows 应用程序,并创建如下图形 Windows Form 界面。
  2. 发布按钮的单击事件如下
  3. private void btnPublish_Click(object sender, EventArgs e)
    {
        lblErrorMsg.Text = String.Empty;
                 
        if (flag && String.IsNullOrEmpty(txtDataSource.Text))
        {
            lblErrorMsg.Text = "Please select DataSource path !!";
            flag = false;
        }
        if (flag && String.IsNullOrEmpty(txtDataSets.Text))
        {
            lblErrorMsg.Text = "Please select DataSets path !!";
            flag = false;
        }
        if (flag && String.IsNullOrEmpty(txtReports.Text))
        {
            lblErrorMsg.Text = "Please select Reports path !!";
            flag = false;
        }
        if (flag && String.IsNullOrEmpty(txtServer.Text))
        {
            lblErrorMsg.Text = "Please select TargetServer path !!";
            flag = false;
        }
        if (flag)
        {               
            lblprogess.Visible = true;
            progressBar1.Visible = true;
            progressBar1.Show();
            progressBar1.Step = 20;
           
            ReportingService2010 rs = new ReportingService2010(); 
            // Example for http://com:8080/abcd/ReportService2010.asmx\
            //rs.Url=txtServer.Text+"/"+"ReportService2010.asmx";
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
            CreateFolders(rs);
            progressBar1.PerformStep();
            CreateDataSource(rs);
            progressBar1.PerformStep();
            createDataSet(rs, txtDataSets.Text);
            progressBar1.PerformStep();
            createReport(rs, txtReports.Text);
            progressBar1.PerformStep();
            moveFiles(rs);
            progressBar1.PerformStep();
            lblprogess.Text = "";
            progressBar1.Hide();
            lblprogess.Visible = false;
            progressBar1.Visible = false;
            SaveIntoFile();
    
        }
        else
        {
          //do something you wish
        }
    }
  4. 在 Reporting Service 中创建文件夹如下
  5. private void CreateFolders(ReportingService2010 rs)
    {
        createFolder(rs, "/", "abcd");
        createFolder(rs, "/abcd", "abcd_REPORTS");
        createFolder(rs, "/abcd", "DATASETS");
        createFolder(rs, "/abcd", "abcd_ODS");
    }
    
    private void createFolder(ReportingService2010 rs,string folderPath, string folderName)
    {
        richTextBox1.SelectionColor  = Color.Green;
        
        richTextBox1.AppendText("\n["+DateTime.Now.ToString(
           "yyyy-MM-dd HH:mm:ss.fff")+"] Creating folder : " + folderName);
        try
        {
            rs.CreateFolder(folderName, folderPath, null);
            richTextBox1.SelectionColor  = Color.Green;
            richTextBox1.AppendText("\n["+DateTime.Now.ToString(
              "yyyy-MM-dd HH:mm:ss.fff")+"] Folder created: "+ folderName + " Successfully");
        }
    
        catch (Exception e)
        {
            richTextBox1.SelectionColor  = Color.Red;
            richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
               "] Error while creating folder : " + folderName);
            richTextBox1.AppendText("\n" + e.Message+"\n");
        }
    }
  6. 创建文件夹后,我们需要上传报告、数据集和数据存储,过程如下所示
    1. 上传报告
    2. private void createReport(ReportingService2010 rs,string reportPath)
      {
          foreach (string fileName in Directory.GetFiles(reportPath,"*.rdl"))
          {
              createReportInServer(rs,Path.GetFileNameWithoutExtension(fileName));
        }
      }
      
      private void createReportInServer(ReportingService2010 rs,string reportName)
      {
          Byte[] definition = null;
          Warning[] warnings = null;
          try
          {
              
              FileStream stream = File.OpenRead(Path.Combine(txtReports.Text,reportName+".rdl"));
              definition = new Byte[stream.Length];
              stream.Read(definition, 0, (int)stream.Length);
              stream.Close();
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                "yyyy-MM-dd HH:mm:ss.fff") + "] Error while reading report : " + reportName);
              richTextBox1.AppendText("\n\n" + e.Message+"\n");
          }
      
          try
          {
              string parent = "/abcd/abcd_ODS";
              CatalogItem report = rs.CreateCatalogItem("Report", reportName, parent,
                          true, definition, null, out warnings);
              
              if (warnings != null)
              {
                  foreach (Warning warning in warnings)
                  {
                      richTextBox1.SelectionColor  = Color.Violet;
                      richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                        "] Warning while creating report : " + reportName);
                      richTextBox1.AppendText("\n" + warning.Message+"\n");
                  }
              }
                  
              else
              {
                  richTextBox1.SelectionColor  = Color.Green;
                  richTextBox1.AppendText("\n[" + 
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                    "] Report: " + reportName + " created successfully with no warnings");
              }
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                 "] Error while creating report : " + reportName);
              richTextBox1.AppendText("\n" + e.Message);
          }
         
      }
    3. 在 Reporting Service 中创建数据集
    4. private void createDataSet(ReportingService2010 rs, string datasetPath)
      {
          foreach (string fileName in Directory.GetFiles(datasetPath,"*.rsd"))
          {
              createDataSetInServer(rs, Path.GetFileNameWithoutExtension(fileName));
          }
      }
      
      private void createDataSetInServer(ReportingService2010 rs, string DataSetName)
      {
          Byte[] definition = null;
          Warning[] warnings = null;
      
          try
          {
      
              FileStream stream = File.OpenRead(Path.Combine(txtDataSets.Text, DataSetName+".rsd"));
              definition = new Byte[stream.Length];
              stream.Read(definition, 0, (int)stream.Length);
              stream.Close();
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                "] Error while reading Dataset : " + DataSetName);
              richTextBox1.AppendText("\n" + e.Message);
          }
          try
          {
              string parent = "/abcd/abcd_ODS";
              CatalogItem dataset = rs.CreateCatalogItem("DataSet", DataSetName, parent,
                          true, definition, null, out warnings);
              if (warnings != null)
              {
                  foreach (Warning warning in warnings)
                  {
                      richTextBox1.SelectionColor  = Color.Violet;
                      richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                        "yyyy-MM-dd HH:mm:ss.fff") + "] Warning while creating dataset : " + DataSetName);
                      richTextBox1.AppendText("\n" + warning.Message+"\n");
                  }
              }
              else
              {
                  richTextBox1.SelectionColor  = Color.Green;
                  richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                    "yyyy-MM-dd HH:mm:ss.fff") + "] DataSet: " + 
                    DataSetName + " created successfully with no warnings");
              }
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n[" + 
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                "] Error while creating dataset : " + DataSetName);
              richTextBox1.AppendText("\n"+e.Message+"\n");
          }
      }
    5. 创建报告数据源并绑定到 SQL Server。
    6. private void CreateDataSource(ReportingService2010 rs)
      {
          richTextBox1.SelectionColor  = Color.Green;
          richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
            "yyyy-MM-dd HH:mm:ss.fff") + "] Creating dataSource....");
              
          string parent = "/abcd/abcd_ODS";
          string name = "abcd_ODS";
       // Define the data source definition.
          DataSourceDefinition definition = new DataSourceDefinition();
          definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated;
          definition.ConnectString = "Data Source=comp_name\\abcd;Initial Catalog=abcd_ODS";
          definition.Enabled = true;
          definition.EnabledSpecified = true;
          definition.Extension = "SQL";
          definition.ImpersonateUserSpecified = false;
          
          definition.WindowsCredentials = true;
      
          try
          {
              rs.CreateDataSource(name, parent, true, definition, null);
              richTextBox1.SelectionColor  = Color.Green;
              richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                "] Data Source " + name + " created at " + parent + " Successfully !!");
          }
      
          catch (Exception ex)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                "yyyy-MM-dd HH:mm:ss.fff") + "] Error While creating DATASOURCE..\n");
              richTextBox1.AppendText(ex.Message);
              richTextBox1.AppendText("\n");
          }
      }

兴趣点 

学习 Windows Reporting Services 手动操作方式,并创建一个编码工具以避免手动上传。

历史 

注意:请查找附件以获取完整代码。

© . All rights reserved.