在报表设计器中设计完成的报表,若要布署到生产环境中,目前有这样几种方式:
    1.使用报表管理器,创建报表目录、数据源,上传报表文件。这种方式仅适合非常少量的报表,或对个别报表进行更新时使用。
    2.使用开发环境的DEPLOY功能,指定TargetServerURL、TargetFolder等属性即可很方便地将所有报表发布到指定环境。这种方式虽然方便,但却要求开发环境,而这是不可能随时随地都能得以满足的。
    3.使用脚本文件和Reporting Services简单对象访问协议(SOAP)API ,只需将安装脚本文件及报表定义文件RDL拷贝到某一目录下,然后指定一些命令参数即可轻松完成报表的发布,当然您必须拥有RS实用工具的运行权限,以及在报表服务器上创建目录和上传文件的权限,而这些权限一般情况下都不成问题,正常情况下,Reporting Services安装完毕后,RS实用工具就已经在默认命令行的path中了。
    联机文件中提供了一个示例的脚本,我对其增加了一些功能,可以判断文件夹及数据源是否已经存在,从而防止重复创建,并可以自动遍历当前目录下的报表文件进行发布。
    注意:该脚本必须用Visual Studio VB.NET语言编写,象Left这样的函数必须完全指定名字空间才能正确编译通过。
'parentFolder是用rs脚本命令行开关-v定义的全局变量

Dim definition As [Byte]() = Nothing
Dim warnings As Warning() = Nothing
Dim parentPath As String = "/" + parentFolder
Dim filePath As String = "" '当前目录

Public 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=parentFolder.toUpper and items(i).Type=1 Then '1=Folder
                FolderExist=True
            
end if
            
if items(i).name.toUpper=parentFolder.toUpper and items(i).Type=5 Then '1=DataSource
                DataSourceExist=True
            
end if
        
next
    
Catch e As SoapException
       Console.
WriteLine(e.Detail.InnerXml.ToString())
    
End Try

    
if FolderExist THEN
        Console.
WriteLine("文件夹已经存在!")
    
ELSE
        
Try
            rs.CreateFolder(parentFolder, 
"/"Nothing)
            Console.
WriteLine("文件夹: {0} 成功创建!", parentFolder)
        
Catch e As Exception
            Console.
WriteLine(e.Message)
        
End Try
    
END IF

    
if FolderExist and DataSourceExist THEN
        Console.
WriteLine("数据源已经存在!")
    
else
        CreateEstateDataSource()
    
end if

    
Try
        
Dim di As DirectoryInfo
        
if filePath="" then di = new DirectoryInfo("."else di = new DirectoryInfo(filePath)
        
Dim files As FileInfo() = di.GetFiles("*.rdl")
        Console.
WriteLine("共 {0} 个报表文件", files.Length)
        
Dim fiNext As FileInfo
        
For Each fiNext In files
            PublishReport(Microsoft.VisualBasic.
Left(fiNext.name,fiNext.name.length-4))
        
Next

    
Catch e As Exception
        Console.
WriteLine("搜索报表文件失败: {0}", e.ToString())
    
End Try

End Sub


Public Sub CreateEstateDataSource()
    
Dim name As String = parentFolder '数据源名与文件夹名相同
    Dim parent As String = "/" + parentFolder

    
Dim definition As New DataSourceDefinition()
    definition.CredentialRetrieval 
= CredentialRetrievalEnum.Store
    definition.UserName
="sa"
    definition.Password="******"
    definition.ConnectString = "data source=(local);initial catalog=" & parentFolder '假设数据库名也与文件夹名相同
    definition.Enabled = True
    definition.EnabledSpecified 
= True
    definition.Extension 
= "SQL"
    definition.ImpersonateUser = False
    definition.ImpersonateUserSpecified 
= True
    definition.Prompt 
= Nothing
    definition.WindowsCredentials 
= False

    
Try
        rs.CreateDataSource(name, parent, 
False, definition, Nothing)
        Console.
WriteLine("数据源: {0} 成功创建!", 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, 
0CInt(stream.Length))
        stream.Close()
    
Catch e As IOException
        Console.
WriteLine(e.Message)
    
End Try

    
Try
        warnings 
= rs.CreateReport(reportName, parentPath, True, definition, Nothing)

        
If Not (warnings Is NothingThen
            
Dim warning As Warning
            
For Each warning In warnings
                Console.
WriteLine(warning.Message)
            
Next warning
        
Else
            Console.
WriteLine("报表: {0} 成功布署!", reportName)
        
End If
    
Catch e As Exception
        Console.
WriteLine(e.Message)
    
End Try
End Sub
 

    将以上脚本(假设为sample.rss)与报表定义文件rdl放在同一目录下,执行类似这样的命令即可:
rs -i sample.rss -s http://localhost/reportserver -v parentFolder="sampleFolder"

posted on 2005-09-07 17:10  愚公  阅读(2017)  评论(3编辑  收藏  举报