962148150 发表于 2015-7-2 11:44:46

SQL Server Reporting Service 报表自动部署 C#程序简化版

  上次了解了可以通过Reporting Service 自带的 rs.exe 命令来批处理自动部署报表,仔细看了下部署脚本的运行模式,基本上是通过调用 ReportingService2005 来进行部署的,那么完全可以通过程序来调用,下面是一个简化版的自动部署工具。

      /**////
      /// 在报表服务器上面部署指定的数据源connectionString
      ///
      /// ReporterService2005
      /// 数据源名称
      /// 部署目录
      /// 数据源连接类型:SQL, OLEDB, ODBC
      /// 连接串
      public static void CreateSampleDataSource(ReportingService2005 rs, string name, string parentPath, string extension, string connectionString)
      {
            DataSourceDefinition dataSourceDefinition = new DataSourceDefinition();
            dataSourceDefinition.CredentialRetrieval = CredentialRetrievalEnum.Integrated;
            dataSourceDefinition.ConnectString = connectionString;
            dataSourceDefinition.Enabled = true;
            dataSourceDefinition.EnabledSpecified = true;
            dataSourceDefinition.Extension = extension;
            dataSourceDefinition.ImpersonateUser = false;
            dataSourceDefinition.ImpersonateUserSpecified = true;
            //Use the default prompt string.
            dataSourceDefinition.Prompt = "";
            dataSourceDefinition.WindowsCredentials = false;

            try
            {
                rs.CreateDataSource(name, parentPath, true, dataSourceDefinition, null);
                Console.WriteLine("Data source {0} created successfully", name);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
      }

      /**////
      /// 在报表服务器上面部署指定的报表reportName
      ///
      /// ReporterService2005
      /// 报表文件所在文件夹爱路径
      /// 报表文件名称(不含.rdl),也即部署在ReportServer中的报表名称
      /// 部署目录
      public static void PublishReport(ReportingService2005 rs, string filePath, string reportName, string parentPath)
      {
            byte[] definition;
            string ext = ".rdl";

            try
            {
                FileStream stream = File.OpenRead(filePath + reportName + ext);
                definition = new byte;
                stream.Read(definition, 0, Convert.ToInt32(stream.Length));
                stream.Close();

                Warning[] warnings = rs.CreateReport(reportName, parentPath, true, definition, null);
                if (warnings != null)
                {
                  foreach (Warning warning in warnings)
                  {
                        Console.WriteLine(warning.Message);
                  }
                }
                else
                {
                  Console.WriteLine("Report: {0} published successfully with no warnings", reportName);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
      }  其中主要通过 rs.CreateDataSource 方法来创建数据源。rs.CreateReport 来创建报表,下面是完整项目,其中Test()方法包含使用例子。大家要注意的是其中添加了Web引用 ,“http://localhost/reportserver/reportservice2005.asmx?wsdl”,并将此Web 引用重命名为 ReportService2005。
  SSRSAPISample.rar
页: [1]
查看完整版本: SQL Server Reporting Service 报表自动部署 C#程序简化版