用脚本发布Reporting Services报表

来源:岁月联盟 编辑:zhu 时间:2007-12-04
Dim mDefinition As [Byte]() = Nothing
Dim mWarnings As Warning() = Nothing
Dim mParentPath As String = "/" + pParentFolder

Public Sub Main()Sub Main()
rs.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim name As String
Dim items As CatalogItem() = Nothing
Dim folderExist, dataSourceExist As Boolean

Try
items = rs.ListChildren("/", True) '列举ReportServer根目录下所有项,判断目标目录是否已存在
Dim i As Integer
For i = 0 To items.length - 1
If items(i).name.toUpper = pParentFolder.toUpper And items(i).Type = 1 Then '1表示Folder
folderExist = True
End If
If items(i).name.toUpper = pParentFolder.toUpper And items(i).Type = 5 Then '5表示DataSource
dataSourceExist = True
End If
Next
Catch e As SoapException
Console.WriteLine(e.Detail.InnerXml.ToString())
End Try

'创建目标目录
If folderExist Then
Console.WriteLine("FOLDER EXISTED!")
Else
Try
rs.CreateFolder(pParentFolder, "/", Nothing)
Console.WriteLine("FOLDER:{0} CREATED SUCCESSFULLY!", pParentFolder)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End If

'创建数据源
If folderExist And dataSourceExist And pOverwriteDataSource = False Then
Console.WriteLine("DATA SOURCE EXISTED!")
Else
CreateDataSource()
End If

Try
Dim di As DirectoryInfo
If pFilePath = "" Then di = New DirectoryInfo(".") Else di = New DirectoryInfo(pFilePath)
Dim files As FileInfo() = di.GetFiles("*.rdl")
Console.WriteLine("TOTAL REPORT FILES: {0}", files.Length)
Dim fiNext As FileInfo
For Each fiNext In files
DeployReport(Microsoft.VisualBasic.Left(fiNext.name, fiNext.name.length - 4))
Next
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub

Public Sub CreateDataSource()Sub CreateDataSource()
Dim name As String = pDsName
Dim parent As String = "/" + pParentFolder

Dim mDefinition As New DataSourceDefinition()
mDefinition.CredentialRetrieval = CredentialRetrievalEnum.Store
mDefinition.UserName = pDsUserName
mDefinition.Password = pDsPwd
mDefinition.ConnectString = "data source=yourservername"
mDefinition.Enabled = True
mDefinition.EnabledSpecified = True
mDefinition.Extension = "ORACLE"
mDefinition.ImpersonateUser = False
mDefinition.ImpersonateUserSpecified = True
mDefinition.Prompt = Nothing
mDefinition.WindowsCredentials = False

Try
rs.CreateDataSource(pDsName, parent, True, mDefinition, Nothing)
Console.WriteLine("DATASOURCE:{0} CREATE SUCCESSFULLY!", name)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub

Public Sub DeployReport()Sub DeployReport(ByVal reportName As String)
Try
Dim stream As FileStream = File.OpenRead(pFilePath + reportName + ".rdl")
mDefinition = New [Byte](stream.Length) {}
stream.Read(mDefinition, 0, CInt(stream.Length))
stream.Close()
Catch e As IOException
Console.WriteLine(e.Message)
End Try

Try
mWarnings = rs.CreateReport(reportName, mParentPath, True, mDefinition, Nothing)

If Not (mWarnings Is Nothing) Then
Dim warning As Warning
For Each warning In mWarnings
Console.WriteLine(warning.Message)
Next warning
Else
Console.WriteLine("DEPLOY REPORT:{0} SUCCESSFULLY!", reportName)
End If
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub



将上述代码保存成.rss文件,如deploy.rss,然后在命令行执行:
C:"Documents and Settings"xxx>rs -i yourrssfilepath"deploy.rss -s http://localhost/reportserver -v pParentFolder="RsSample" -v pOverwriteDataSource=true -v pDsName="DataSource1" -v pDsUserName="youroracleusername" -v pDsPwd=" youroracleuserpassword " -v pFilePath="yourreportfilespath"

输出:
folder existed
datasource:DataSource1 create successfully
total report files: 1
deploy report:Report1 successfully
命令已成功完成