使用rs.exe自動佈署報表

使用rs需用vb語言撰寫指令碼(存成rss)

建立好rdl檔
開啟文字檔並複製下方程式碼,存為rss檔 (路徑自行修改)
開啟文字檔並貼上
rs -i rss檔名 -s ReportServer的URL -u 使用者 -p 密碼
(例如rs -i deploy.rss -s http://127.0.0.1/ReportServer -u aaa -p aaa)


rss內容
Dim definition As [Byte]() = Nothing
Dim warnings As Warning() = Nothing
'資料夾名稱
Dim datasourceFolderName As String = "Data Sources"
Dim datasourceFolderPath As String = "/" + datasourceFolderName
'報表資料夾名稱,所有發佈的報表放此
Dim reportsFolderName As String = "DeployReports"
Dim reportsFolderPath As String = "/" + reportsFolderName
資料來源名稱
Dim DataSourceName As String = "DeteSource"
'本機上設計好的報表存放資料夾
Dim filePath As String = "C:\createRDL\"
'連線字串(資料來源)
Dim connectionString As String = "Data Source=localhost;Initial Catalog=DataBaseName;Connect Timeout=120"


   

Public Sub Main()
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials
    '建立報表資料夾
    CreateReportFolder(reportsFolderName)
    '建立DataSource資料夾
    CreateReportFolder(datasourceFolderName)
    '建立資料來源
    CreateDataSource(datasourceFolderPath)
   
    '部署報表
    DeployRepors(filePath)
Console.Read()
End Sub

'建立資料夾
Public Sub CreateReportFolder(ByVal reportFolder As String)

    Try
        'Create Reports Folder
        rs.CreateFolder(reportFolder,"/",Nothing)
       
        Console.WriteLine("Parent Folder Created:{0}",reportFolder)
    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try

End Sub

'部署報表
Public Sub DeployRepors(ByVal filePath As String)

    Dim tempFileAry As String()
    Dim reportFilePath as String
    tempFileAry = Directory.GetFiles(filepath)
    For Each reportFilePath In tempFileAry
        Dim fileName As String
        fileName = reportFilePath.Substring(reportFilePath.LastIndexOf("\") + 1)
        Dim fileSuffix As String
        fileSuffix = fileName.Substring(fileName.LastIndexOf(".") + 1)


        Select Case fileSuffix
            Case "rdl","png"
                PublishReport(fileName,fileSuffix)
        End Select
    Next

End Sub
'******************************************************************
'*****           建立資料來源                          **************
'*******************************************************************
Public Sub CreateDataSource(ByVal sourceFolder As String)

    Dim name As String = DataSourceName
   'Define the data source definition.
    Dim definition As New DataSourceDefinition()
    Dim dSource As New DataSource()
   
    dsource.Item=definition
   
    definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
    definition.ConnectString =connectionString
    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 = true
   
    dsource.Name=datasourceFolderPath

    Try
       
        rs.CreateDataSource(name, sourceFolder, true, definition, Nothing)
            Console.WriteLine("Created DataSource:{0}",name)
    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try

End Sub


'******************************************************
'******* 發佈報表 *********
'*******************************************************

Public Sub PublishReport(ByVal reportName As String,ByVal fileSuffix As String)
     
    Try

        Dim stream As FileStream = File.OpenRead(filePath + reportName)
        definition= New [Byte](stream.Length-1) {}
        stream.Read(definition, 0, CInt(stream.Length))
        stream.Close()
    Catch e As IOException
        Console.WriteLine(e.Message)
    End Try
    Try
      '**********************   parentPath
      If(fileSuffix="rdl") Then
Console.WriteLine(reportsFolderPath)
Console.WriteLine(definition)
Console.Read()
        warnings = rs.CreateReport(reportName,reportsFolderPath, true, definition, Nothing)
         SetReportDataSourceRef(reportName)
        
        Else If(fileSuffix="png") Then
                rs.CreateResource(reportName,reportsFolderPath,true,definition,"png",Nothing) 
      End If
        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)
Console.Read()
    End Try
End Sub

'************************************************************************
'************* 設定報表的資料來源     **************************************
'************************************************************************
Public Sub SetReportDataSourceRef(ByVal reportName As String)
Try
      Dim reference As DataSourceReference = New DataSourceReference
      Dim ds As DataSource = New DataSource
      reference.Reference=datasourceFolderPath+"/" + DataSourceName
       Dim dsArray As DataSource()=rs.GetItemDataSources(reportsFolderPath+"/"+reportName)
      
      ds=dsArray(0)
       ds.Item = CType(reference, DataSourceReference)
      rs.SetItemDataSources(reportsFolderPath+"/"+reportName, dsArray)

     Console.Read()
Catch _exception As Exception
     Console.WriteLine(_exception)
Console.Read()
 End Try
End Sub









http://msdn.microsoft.com/zh-tw/library/ms152874(v=sql.105).aspx
http://msdn.microsoft.com/zh-tw/library/ms152908(v=sql.105).aspx
http://msdn.microsoft.com/zh-tw/library/ms162839(v=sql.105).aspx
http://www.blogjava.net/wujun/archive/2006/11/27/83463.html

留言

這個網誌中的熱門文章

SQL SEVER備份與還原遭遇問題

asp.net頁面間傳值