SQL Server Reporting Service 报表自动部署 命令行
SQL Server Reporting Service 提供了一个名为 RS.exe 的小工具(默认情况下安装在C:"Program Files"Microsoft SQL Server"90"Tools"Binn 下),使用-?命令获取一些帮助信息
D:">rs -?
Microsoft (R) Reporting Services RS
Version 9.00.1399.00 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
Mgmt2000 - (Deprecated) The ReportService 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
Microsoft (R) Reporting Services RS
Version 9.00.1399.00 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
Mgmt2000 - (Deprecated) The ReportService 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 指定执行的脚本文件,以及 -s 指定需要部署的 ReportServer 地址。脚本文件在安装 Microsoft SQL Server 2005 Samples 后,可以在 C:"Program Files"Microsoft SQL Server"90"Samples"ReportinServices"Script Samples 下找到(*.rss),下面显示 PublishSampleReports.rss
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""
Public 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
Public 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
Public Sub PublishReport(ByVal reportName As String)
Try
Dim stream As FileStream = File.OpenRead(filePath + reportName + ".rdl")
definition = New [Byte](stream.Length) {}
stream.Read(definition, 0, CInt(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 Nothing) Then
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
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""
Public 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
Public 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
Public Sub PublishReport(ByVal reportName As String)
Try
Dim stream As FileStream = File.OpenRead(filePath + reportName + ".rdl")
definition = New [Byte](stream.Length) {}
stream.Read(definition, 0, CInt(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 Nothing) Then
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
注意修改 filePath(当前 .rdl 存储位置),以及把CreateSampleDataSource 的参数改为与实际数据库一致。然后使用命令:
RS -i "PublishReports.rss" -s "http://localhost/reportserver"
就可以把报表部署到 ReportServer 了


浙公网安备 33010602011771号