2008-09-17 188 views
32

我需要爲部署SQL Server Reporting Services報告創建可重複的過程。我不贊成使用Visual Studio和/或Business Development Studio來執行此操作。腳本部署的rs.exe方法似乎也相當笨拙。有沒有人有一個非常優雅的方式,他們已經能夠部署報告。這裏的關鍵是我希望過程完全自動化。Reporting Services部署

回答

32

我們使用rs.exe,一旦我們開發了腳本,我們不再需要觸摸它,它只是工作。這裏是源代碼(我稍微修改了它來手動刪除敏感數據,但沒有機會測試它,希望我沒有制動任何東西),它從不同語言的子目錄中部署報告和相關圖像。數據源也被創建。

'===================================================================== 
' File:  PublishReports.rss 
' 
' Summary: Script that can be used with RS.exe to 
'   publish the reports. 
' 
' Rss file spans from beginnig of this comment to end of module 
' (except of "End Module"). 
'===================================================================== 

Dim langPaths As String() = {"en", "cs", "pl", "de"} 
Dim filePath As String = Environment.CurrentDirectory 

Public Sub Main() 

    rs.Credentials = System.Net.CredentialCache.DefaultCredentials 

    'Create parent folder 
    Try 
     rs.CreateFolder(parentFolder, "/", Nothing) 
     Console.WriteLine("Parent folder created: {0}", parentFolder) 
    Catch e As Exception 
     Console.WriteLine(e.Message) 
    End Try 

    PublishLanguagesFromFolder(filePath) 

End Sub 

Public Sub PublishLanguagesFromFolder(ByVal folder As String) 
    Dim Lang As Integer 
    Dim langPath As String 

    For Lang = langPaths.GetLowerBound(0) To langPaths.GetUpperBound(0) 
     langPath = langPaths(Lang) 

     'Create the lang folder 
     Try 
      rs.CreateFolder(langPath, "/" + parentFolder, Nothing) 
      Console.WriteLine("Parent lang folder created: {0}", parentFolder + "/" + langPath) 
     Catch e As Exception 
      Console.WriteLine(e.Message) 
     End Try 

     'Create the shared data source 
     CreateDataSource("/" + parentFolder + "/" + langPath) 

     'Publish reports and images 
     PublishFolderContents(folder + "\" + langPath, "/" + parentFolder + "/" + langPath) 
    Next 'Lang 
End Sub 

Public Sub CreateDataSource(ByVal targetFolder As String) 
    Dim name As String = "data source" 

    'Data source definition. 
    Dim definition As New DataSourceDefinition 
    definition.CredentialRetrieval = CredentialRetrievalEnum.Store 
    definition.ConnectString = "data source=" + dbServer + ";initial catalog=" + db 
    definition.Enabled = True 
    definition.EnabledSpecified = True 
    definition.Extension = "SQL" 
    definition.ImpersonateUser = False 
    definition.ImpersonateUserSpecified = True 
    'Use the default prompt string. 
    definition.Prompt = Nothing 
    definition.WindowsCredentials = False 
    'Login information 
    definition.UserName = "user" 
    definition.Password = "password" 

    Try 
    'name, folder, overwrite, definition, properties 
     rs.CreateDataSource(name, targetFolder, True, definition, Nothing) 
    Catch e As Exception 
     Console.WriteLine(e.Message) 
    End Try 

End Sub 

Public Sub PublishFolderContents(ByVal sourceFolder As String, ByVal targetFolder As String) 
    Dim di As New DirectoryInfo(sourceFolder) 
    Dim fis As FileInfo() = di.GetFiles() 
    Dim fi As FileInfo 

    Dim fileName As String 

    For Each fi In fis 
     fileName = fi.Name 
     Select Case fileName.Substring(fileName.Length - 4).ToUpper 
      Case ".RDL" 
       PublishReport(sourceFolder, fileName, targetFolder) 
      Case ".JPG", ".JPEG" 
       PublishResource(sourceFolder, fileName, "image/jpeg", targetFolder) 
      Case ".GIF", ".PNG", ".BMP" 
       PublishResource(sourceFolder, fileName, "image/" + fileName.Substring(fileName.Length - 3).ToLower, targetFolder) 
     End Select 
    Next fi 
End Sub 

Public Sub PublishReport(ByVal sourceFolder As String, ByVal reportName As String, ByVal targetFolder As String) 
    Dim definition As [Byte]() = Nothing 
    Dim warnings As Warning() = Nothing 

    Try 
     Dim stream As FileStream = File.OpenRead(sourceFolder + "\" + reportName) 
     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 
    'name, folder, overwrite, definition, properties 
     warnings = rs.CreateReport(reportName.Substring(0, reportName.Length - 4), targetFolder, True, 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", targetFolder + "/" + reportName) 
     End If 
    Catch e As Exception 
     Console.WriteLine(e.Message) 
    End Try 
End Sub 

Public Sub PublishResource(ByVal sourceFolder As String, ByVal resourceName As String, ByVal resourceMIME As String, ByVal targetFolder As String) 
    Dim definition As [Byte]() = Nothing 
    Dim warnings As Warning() = Nothing 

    Try 
     Dim stream As FileStream = File.OpenRead(sourceFolder + "\" + resourceName) 
     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 
    'name, folder, overwrite, definition, MIME, properties 
     rs.CreateResource(resourceName, targetFolder, True, definition, resourceMIME, Nothing) 
     Console.WriteLine("Resource: {0} with MIME {1} created successfully", targetFolder + "/" + resourceName, resourceMIME) 
    Catch e As Exception 
     Console.WriteLine(e.Message) 
    End Try 
End Sub 

這裏是批量調用rs.exe:

SET ReportServer=%1 
SET DBServer=%2 
SET DBName=%3 
SET ReportFolder=%4 

rs -i PublishReports.rss -s %ReportServer% -v dbServer="%DBServer%" -v db="%DBName%" -v parentFolder="%ReportFolder%" >PublishReports.log 2>&1 

pause 
+0

嗨。我試圖將這個腳本用於我自己的報告服務部署。問題..我如何在我的計算機上組織目錄結構?我想爲每個報告子目錄都有一個vs項目嗎? – BigJoe714 2009-02-16 14:09:42

+0

這與VS項目無關。該腳本預期每個語言突變的子文件夾(例如,en,de,參見腳本開始)。每個語言文件夾都包含使用的rdl文件和資源(通常是由報告使用的圖像)。 – 2009-03-10 13:49:50

0

我知道你說你不贊成業務開發工作室來做這件事,但我發現內置工具非常可靠並且易於使用。

+2

哦,我同意。問題是我真的希望部署完全自動化。 – Bart 2008-09-17 23:40:50

+0

啊。我們只部署到測試和生產服務器,並且只有在更改完成後,才能使用內置工具對我們非常有效(我猜我們很幸運) – cori 2008-09-18 14:49:12

1

那麼不是很優雅。我們創建了使用reportingservices2005 Web服務的自己的工具。我們發現這是獲得我們想要的最可靠的方式。

這不是那麼困難,並且可以讓您擴展它來完成其他事情,如根據需要創建數據源和文件夾。

0

您是否研究過任何持續集成解決方案(例如CruiseControl.NET)?如果您能夠使用rs.exe部署報告,那麼您可以在CruiseControl中設置一個自動化流程,以在計時器上或每當報告被修改時構建和部署您的報告。

1

我強烈建議RSScripter。正如概覽指出:

報告服務的編劇是一個.NET Windows窗體應用程序,使 腳本和所有 的Microsoft SQL Server的傳遞報告 服務目錄項 以幫助從一臺服務器轉移到他們另一個是 。它也可以用於輕鬆地將 質量上的項目從一個報告 服務文件夾移動到同一臺 服務器上的另一個文件夾。根據腳本選擇 選項,報告服務 的編劇也可傳送所有目錄 項目性質,例如描述, 歷史選項,執行選項 (包括特定的報告和共享 調度),訂閱(正常和 數據驅動)和服務器端報告 參數。

8

我使用的腳本@David提供,但我不得不添加一些代碼(我打字這件事作爲一個答案,因爲這將是太長了評論

的問題是:如果有已經是在報告定義中附加到報告的「共享數據源」,這不會與在腳本中創建的數據源相同。

這還從由「CREATEREPORT」方法發出的警告變得明顯:

該數據集「」指的是共享數據源「」,其未在報告服務器上發佈。

因此,數據源必須事後明確設置。我做了下面的代碼更改:

我添加了一個全局變量:

Dim dataSourceRefs(0) As DataSource 

在的createDataSource方法結束時,該變量得到填補:

Dim dsr As New DataSourceReference 
dsr.Reference = "/" + parentFolder + "/" + db 
Dim ds As New DataSource 
ds.Item = CType(dsr, DataSourceDefinitionOrReference) 
ds.Name = db 
dataSourceRefs(0) = ds 

而在PublishReport方法,該數據源被明確設置(在調用CreateReport之後):

rs.SetItemDataSources(targetFolder + "/" + reportName.Substring(0, reportName.Length - 4), dataSourceRefs) 

請注意,最後一次調用僅爲RS 2005或更高版本。如果你想你的報告加載到RS 2000的服務器,你必須代替使用套裝報告數據源:

rs.SetReportDataSources(targetFolder + "/" + reportName.Substring(0, reportName.Length - 4), dataSourceRefs)