SQL Server Reporting Service - 命令行部署脚本介绍

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,独享型 2核4GB
简介: 首先应公司老总要求写了英文版: http://www.cnblogs.com/WilsonWu/archive/2009/02/19/1394200.html 英文不好别笑话. 进入正题! 使用 SQL Server Reporting Service 的朋友应该都会遇到与此类似的问题, 尤其...

首先应公司老总要求写了英文版: http://www.cnblogs.com/WilsonWu/archive/2009/02/19/1394200.html

英文不好别笑话.

进入正题!

使用 SQL Server Reporting Service 的朋友应该都会遇到与此类似的问题, 尤其是在产品中, 比如我们有若干个报表形成的一个产品, 这些报表是需要用户部署在报表服务器上的才能使用的, 我们可以只给用户 RDL 报表文件和一个文档教程, 让他们自己上传, 自己配置数据源等等, 但是这样也许令人感到这个产品太不人性化, 而 SQL Server Reporting Service 也想到了这点, 它给我们提供了一个名为 RS.exe 的小工具, 这个工具就可以帮助我们以命令行的形式做一些有关 Reporting Service 的工作, 现在先介绍一下 RS.exe 工具中的主要功能:

首先使用-?命令获取一些帮助信息:

D:\RS>rs -? 
Microsoft (R) Reporting Services RS 
Version 
10.0.1600.22  ((SQL_PreRelease) .080709 - 1414  ) x86 
Executes script file contents against the specified Report Server. 
RS -i inputfile -s serverURL 
[ -u username ]   [ -p password ]  
   
[ -l timeout ]   [ -b ]   [ -e endpoint ]   [ -v var=value ]   [ -t ]  

        -i  inputfile   Script file to execute 
        -s  serverURL   URL (including server and vroot) to execute 
                        script against. 
        -u  username    User name used to log in to the server. 
        -p  password    Password used to log in to the server. 
        -e  endpoint    Web service endpoint to use with the script. 
                        Options are: 
                        Exec2005 - The ReportExecution2005 endpoint 
                        Mgmt2005 - The ReportService2005 endpoint 
        -l  timeout     Number of seconds before the connection to the 
                        server times out. Default is 
60  seconds and  0  is 
                        infinite time out. 
        -b              Run as a batch and rollback if commands fail 
        -v  var
= value   Variables and values to pass to the script 
        -t  trace       Include trace information in error message

 

其实看上去也不难, 其中重点介绍一个参数, 大家可以看到-i这个参数中需要传输一个脚本文件给该工具, 其实这个脚本文件也是一般的VB.NET语法写的, 在网上查了一些资料, SQL Server 安装包中没有默认包含脚本实例, 所以我们需要自己下载一下, 大家可以通过: http://www.codeplex.com/MSFTRSProdSamples 这个网站下载并安装, 其中会下载到一个安装包, 它包含了所有 SQL Server 中技术的实例, 安装完成后, 我们进入C:\Program Files\Microsoft SQL Server\90\Samples\Reporting Services 目录, 然后找到一个名为 "Script Samples" 的目录, 这个目录中有若干个RSS为后缀的文件, 这些文件就是脚本文件里, 打开一个名为PublishSampleReports.rss看一下, 内容如下:

' ============================================================================= 
'
  File:      PublishSampleReports.rss 
'
 
'
  Summary:  Demonstrates a script that can be used with RS.exe to 
'
         publish the sample reports that ship with Reporting Services. 
'
 
'
--------------------------------------------------------------------- 
'
 This file is part of Microsoft SQL Server Code Samples. 
'
 
'
  Copyright (C) Microsoft Corporation.  All rights reserved. 
'
 
'
 This source code is intended only as a supplement to Microsoft 
'
 Development Tools and/or on-line documentation.  See these other 
'
 materials for detailed information regarding Microsoft code samples. 
'
 
'
 THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
'
 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
'
 IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
'
 PARTICULAR PURPOSE. 
'
============================================================================= 
'
 
'
 1.0 Documentation 
'
 
'
 Read the following in order to familiarize yourself with the sample script. 
'
 
'
 1.1 Overview 
'
 
'
 This sample script uses a script file (.rss) and the script environment to run 
'
 Web service operations on a specified report server. The script creates a folder 
'
 that you specify as a command-prompt variable using the 杤 switch, and then 
'
 publishes the sample reports that ship with Reporting Services to a report server. 
'
 Depending on the location of your sample reports, you may need to modify the 
'
 value of the filePath variable, which references the path to your sample reports. 
'
 
'
 1.2 Script Variables 
'
 
'
 Variables that are passed on the command line with the -v switch: 
'
 
'
 (a) parentFolder - corresponds to the folder that the script creates and uses 
'
     to contain your published reports 
'
 
'
 1.3 Sample Command Lines 
'
 
'
 
'
 1.3.1 Use the script to publish the sample reports to an AdventureWorks Sample Reports folder. 
'
 
'
       rs -i PublishSampleReports.rss -s http://myserver/reportserver 
'
 

Dim  definition  As  [ Byte ]()  =   Nothing  
Dim  warnings  As  Warning()  =   Nothing  
Dim  parentFolder  As   String   =   " AdventureWorks Sample Reports "  
Dim  parentPath  As   String   =   " / "   +  parentFolder 
Dim  filePath  As   String   =   " C:\Program Files\Microsoft SQL Server\90\Samples\Reporting Services\Report Samples\AdventureWorks Sample Reports\ "  

img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
Public   Sub Main() Sub Main() 

    rs.Credentials 
= System.Net.CredentialCache.DefaultCredentials 
    
'Create the parent folder 
    Try 
        rs.CreateFolder(parentFolder, 
"/"Nothing
        Console.WriteLine(
"Parent folder {0} created successfully", parentFolder) 
    
Catch e As Exception 
        Console.WriteLine(e.Message) 
    
End Try 

    
'Create the AdventureWorks shared data source 
    CreateSampleDataSource("AdventureWorks""SQL""data source=(local);initial catalog=AdventureWorks"
    CreateSampleDataSource(
"AdventureWorksDW""OLEDB-MD", _ 
        
"data source=localhost;initial catalog=Adventure Works DW"

    
'Publish the sample reports 
    PublishReport("Company Sales"
    PublishReport(
"Employee Sales Summary"
    PublishReport(
"Product Catalog"
    PublishReport(
"Product Line Sales"
    PublishReport(
"Sales Order Detail"
    PublishReport(
"Territory Sales Drilldown"

End Sub
 

img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
Public   Sub CreateSampleDataSource() Sub CreateSampleDataSource(name As String, extension As String, connectionString As String
    
'Define the data source definition. 
    Dim definition As New DataSourceDefinition() 
    definition.CredentialRetrieval 
= CredentialRetrievalEnum.Integrated 
    definition.ConnectString 
= connectionString 
    definition.Enabled 
= True 
    definition.EnabledSpecified 
= True 
    definition.Extension 
= extension 
    definition.ImpersonateUser 
= False 
    definition.ImpersonateUserSpecified 
= True 
    
'Use the default prompt string. 
    definition.Prompt = Nothing 
    definition.WindowsCredentials 
= False 

Try 
    rs.CreateDataSource(name, parentPath, 
False, definition, Nothing
    Console.WriteLine(
"Data source {0} created successfully", name) 

Catch e As Exception 
    Console.WriteLine(e.Message) 
End Try 
End Sub
 

img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
Public   Sub PublishReport() Sub PublishReport(ByVal reportName As String
    
Try 
        
Dim stream As FileStream = File.OpenRead(filePath + reportName + ".rdl"
        definition 
= New [Byte](stream.Length) {} 
        stream.Read(definition, 
0CInt(stream.Length)) 
        stream.Close() 

    
Catch e As IOException 
        Console.WriteLine(e.Message) 
    
End Try 

    
Try 
        warnings 
= rs.CreateReport(reportName, parentPath, False, definition, Nothing

        
If Not (warnings Is NothingThen 
            
Dim warning As Warning 
            
For Each warning In warnings 
                Console.WriteLine(warning.Message) 
            
Next warning 

        
Else 
            Console.WriteLine(
"Report: {0} published successfully with no warnings", reportName) 
        
End If 

    
Catch e As Exception 
        Console.WriteLine(e.Message) 
    
End Try 
End Sub
 

 

除了一些专有的rs链接服务器的操作, 大部分代码还是很清晰的, 而且发布数据源和报表都单独列为一个方法, 我们只要摘取其中的内容, 建立我们的新报表就可以完成一个可用的发布报表的脚本了, 这里我不写了, 如果懒得下载的朋友直接使用上面的代码也是一样的.

然后使用:

RS  - " PublishReports.rss "   - " http://[ReportServer]/ReportServer/ "  

命令就可以执行报表部署操作了.

介绍就到这里, 我将在近期发布一篇关于TFS和报表服务技巧的文章, 其中会具体给大家一个实例代码, 敬请期待! 感谢!

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
3天前
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
25 13
|
7天前
|
存储 SQL C++
对比 SQL Server中的VARCHAR(max) 与VARCHAR(n) 数据类型
【7月更文挑战7天】SQL Server 中的 VARCHAR(max) vs VARCHAR(n): - VARCHAR(n) 存储最多 n 个字符(1-8000),适合短文本。 - VARCHAR(max) 可存储约 21 亿个字符,适合大量文本。 - VARCHAR(n) 在处理小数据时性能更好,空间固定。 - VARCHAR(max) 对于大文本更合适,但可能影响性能。 - 选择取决于数据长度预期和业务需求。
|
13天前
|
SQL API Python
`bandit`是一个Python静态代码分析工具,专注于查找常见的安全漏洞,如SQL注入、跨站脚本(XSS)等。
`bandit`是一个Python静态代码分析工具,专注于查找常见的安全漏洞,如SQL注入、跨站脚本(XSS)等。
|
11天前
|
SQL Oracle 关系型数据库
MySQL、SQL Server和Oracle数据库安装部署教程
数据库的安装部署教程因不同的数据库管理系统(DBMS)而异,以下将以MySQL、SQL Server和Oracle为例,分别概述其安装部署的基本步骤。请注意,由于软件版本和操作系统的不同,具体步骤可能会有所变化。
45 3
|
17天前
|
SQL 存储 安全
数据库数据恢复—SQL Server数据库出现逻辑错误的数据恢复案例
SQL Server数据库数据恢复环境: 某品牌服务器存储中有两组raid5磁盘阵列。操作系统层面跑着SQL Server数据库,SQL Server数据库存放在D盘分区中。 SQL Server数据库故障: 存放SQL Server数据库的D盘分区容量不足,管理员在E盘中生成了一个.ndf的文件并且将数据库路径指向E盘继续使用。数据库继续运行一段时间后出现故障并报错,连接失效,SqlServer数据库无法附加查询。管理员多次尝试恢复数据库数据但是没有成功。
|
22天前
|
SQL 存储 关系型数据库
关系型数据库SQL Server学习
【7月更文挑战第4天】
26 2
|
27天前
|
SQL 存储 测试技术
|
26天前
|
SQL 机器学习/深度学习 搜索推荐
SQL SERVER 转换失败
【6月更文挑战第25天】
|
5天前
|
SQL 存储 文件存储
快速部署sqlserver AlwaysOn集群
【7月更文挑战第8天】快速部署SQL Server AlwaysOn集群概览: 1. 准备工作:确认硬件与软件兼容,操作系统一致,资源充足;各节点安装相同SQL Server版本;配置静态IP,保障网络稳定。 2. 创建WFC:安装集群功能,通过管理器创建集群,设定名称、IP及节点。 3. 配置共享存储:接入SAN/NAS,将其作为集群资源。 4. 启用AlwaysOn:在SQL Server中开启功能,创建可用性组,定义主辅副本,添加数据库,设置侦听器。 5. 测试验证:故障转移测试,检查数据同步与连接稳定性。 部署前需深入理解技术细节并测试。
|
5天前
|
SQL 监控 数据库
SQL Server 查询超时问题排查
【7月更文挑战第8天】排查 SQL Server 查询超时涉及五个主要方面:检查复杂查询、评估服务器性能、审视配置参数、更新统计信息和分析执行计划。关注点包括查询的结构(如连接、子查询和索引),服务器资源(CPU、内存、网络延迟),连接和内存设置,以及统计信息的时效性。通过这些步骤可定位并解决性能瓶颈。