Reporting Services报表的自动部署

因为项目中Reporting Services报表需要自动部署,所以网上找了找

有现成的代码,但发现它还是有点问题

于是动手把它做了修改。已适应项目的需求

脚本如下

Dim definition As [Byte]() = Nothing
Dim warnings As Warning() = Nothing
'远程报表数据源所在目录名,不支持中文 [此项需要修改]
Dim datasourceFolderName As String = "Data Sources"

'数据源名称 [此项需要修改]
Dim datasourcefileName As String = "PTDS_PW_conn"

'远程报表数据源所在目录路径
Dim datasourceFolderPath As String = "/" + datasourceFolderName

'报表目录名  所有发布的报表放在此目录 [此项需要修改]
Dim reportsFolderName As String = "PW"

'报表上级目录名  ,根目录为"/",只支持根目录以及根目录下一层.即报表上级目录名最多为如/aaa/   [此项需要修改]
Dim reportsfatherPath As String = "/ptds_reports"

'报表目录名  所有发布的报表放在此目录路径
Dim reportsFolderPath As String = reportsfatherPath +"/"+ reportsFolderName

'设计好的报表 存放的路径.即本地存放的报表路径 [此项需要修改]
Dim filePath As String = "E:\Report Project1\"

'链接字符串 [此项需要修改]
Dim connectionString As String = "Data Source=192.168.1.4;Initial Catalog=PTDS3;Connect Timeout=120"

'数据库用户名 [此项需要修改]
Dim UsernameString As String = "sa"

'数据库密码 [此项需要修改]
Dim PasswordString As String = "sa12345"

Public Sub Main()
reportsFolderPath=reportsFolderPath.Replace("//", "/")
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials
    '创建报表目录,参数代表,(文件夹名,文件夹所在目录)
    CreateReportFolder(reportsFolderName,reportsfatherPath)
    '创建DataSource 目录
    CreateReportFolder(datasourceFolderName,datasourceFolderPath)
    '创建数据源
    CreateDataSource(datasourceFolderPath)
    '部署报表
    DeployRepors(filePath)
End Sub

'创建目录
Public Sub CreateReportFolder(ByVal reportFolder As String,ByVal reportpath As String)
    Dim pathname As String
    Try
        'Create Reports Folder
   If (reportpath.Length>1) then
   Try
pathname = reportpath.Substring(reportpath.LastIndexOf("/") + 1)
  rs.CreateFolder(pathname,"/",Nothing)
Console.WriteLine("Create Folder success:{0}",pathname)
Catch e As Exception
        Console.WriteLine("Create Folder Error.But is does not influence  the Application"+e.message)
    End Try
End If
        rs.CreateFolder(reportFolder,reportpath,Nothing)
        Console.WriteLine("Create Folder success:{0}",reportFolder)
    Catch e As Exception
        Console.WriteLine("Create Folder Error.But is does not influence  the Application"+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 = datasourcefileName
   'Define the data source definition.
    Dim definition As New DataSourceDefinition()
    Dim dSource As New DataSource()
    dsource.Item=definition
    definition.CredentialRetrieval = CredentialRetrievalEnum.Store
    definition.ConnectString =connectionString
    definition.UserName=UsernameString
    definition.Password=PasswordString
    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
    dsource.Name=datasourceFolderPath

    Try
        rs.CreateDataSource(name, sourceFolder, true, definition, Nothing)
            Console.WriteLine("Create Data Source:{0} ERROR",name)
    Catch e As Exception
        Console.WriteLine("Create Data Source error"+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) {}
        stream.Read(definition, 0, CInt(stream.Length))
        stream.Close()
    Catch e As IOException
        Console.WriteLine("report")
    End Try

    Try
      '**********************   parentPath
      If(fileSuffix="rdl") Then
Dim str_reportname as String
str_reportname=reportName.Substring(0,reportName.LastIndexOf("."))
  Console.WriteLine("str_reportname: {0} ", str_reportname)
        warnings = rs.CreateReport(str_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:"+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

'************************************************************************
'************* 设置报表数据源      **************************************
'************************************************************************
Public Sub SetReportDataSourceRef(ByVal reportName As String)
Try
      Dim reference As DataSourceReference = New DataSourceReference
      Dim ds As DataSource = New DataSource
      reference.Reference=datasourceFolderPath+"/"+datasourcefileName
Dim str_reportname as String
str_reportname=reportName.Substring(0,reportName.LastIndexOf("."))
       Dim dsArray As DataSource()=rs.GetItemDataSources(reportsFolderPath+"/"+str_reportname)
      ds=dsArray(0)
       ds.Item = CType(reference, DataSourceReference)

      rs.SetItemDataSources(reportsFolderPath+"/"+str_reportname,dsArray)
Catch _exception As Exception
     Console.WriteLine("aaa"+_exception.message)
End Try
End Sub

 

使用说明:保存上面的脚本为deploy.rss

新建deploy.dat 批处理文件 运行上面脚本

deploy.bat 内容

rs -i deploy.rss -s http://192.168.0.88/ReportServer -u userName -p password

双击运行deploy.bat 报表将自动发布到http://192.168.0.88/ReportServer 服务器上。

 

 

rs.exe参数说明

RUN --CMD
C:\Documents and Settings\junmy>rs /?
Microsoft (R) Reporting Services RS
版本 9.00.1399.00 x86
根据指定的报表服务器执行脚本文件内容。
RS -i 输入文件 -s serverURL [-u 用户名] [-p 密码]
   [-l 超时] [-b] [-e 端点] [-v var=value] [-t]

        -i  输入文件    要执行的脚本文件
        -s  serverURL   执行脚本
                        所依据的 URL (包括服务器和 vroot)。
        -u  用户名      用于登录到服务器中的用户名。
        -p  密码        用于登录到服务器中的密码。
        -e  端点        要与脚本一起使用的 Web 服务端点。
                        选项为:
                        Exec2005 - ReportExecution2005 端点
                        Mgmt2005 - ReportService2005 端点
                        Mgmt2000 - (不推荐使用) ReportService 端点
        -l  超时        连接到服务器之前
                        超时的秒数。默认值为 60 秒,0 表示
                        无限长的超时。
        -b              作为批进行运行,且如果命令失败则回滚
        -v  var=value   传递给脚本的变量和值
        -t  跟踪        在错误消息中包含跟踪信息

posted on 2008-01-08 23:24  huihui-热带鱼  阅读(640)  评论(0编辑  收藏  举报