轉帖|其它|編輯:郝浩|2009-02-20 09:32:29.000|閱讀 1127 次
概述:SQL Server Reporting Service 報表命令行部署腳本的相關知識。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
使用 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 安裝包中沒有默認包含腳本實例, 所以我們需要自己下載一下, 大家可以通過: 這個網站下載并安裝, 其中會下載到一個安裝包, 它包含了所有 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 //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\"

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
除了一些專有的rs鏈接服務器的操作, 大部分代碼還是很清晰的, 而且發布數據源和報表都單獨列為一個方法, 我們只要摘取其中的內容, 建立我們的新報表就可以完成一個可用的發布報表的腳本了, 這里我不寫了, 如果懶得下載的朋友直接使用上面的代碼也是一樣的.
然后使用:
RS -i "PublishReports.rss" -s "//[ReportServer]/ReportServer/" 命令就可以執行報表部署操作了.
介紹就到這里, 我將在近期發布一篇關于TFS和報表服務技巧的文章, 其中會具體給大家一個實例代碼, 敬請期待! 感謝!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:博客園