blogjava's web log

blogjava's web log
...

SqlServer 2005 报表自动部署



当我们需要把设计好的报表发布到Web服务器上时,如果用VS2005只需要 右击报表工程,单击Deploy就可轻松搞定,如果不用VS2005发布,该怎么办?

解决办法有2种。
1.引用 sqlserver2005 的web服务。根据他提供的方法发布报表
2.用sqlserver2005 的rs.exe 命名发布。

我这里介绍的是根据 rs.exe这个命令来自动发布报表的。脚本已经写好,直接Copy 稍微的改动一下即可!

1.现看看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  跟踪        在错误消息中包含跟踪信息

C:\Documents and Settings\junmy>
 
2.拷贝下面脚本保存deployReport.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 = "testDeployReports"
Dim reportsFolderPath As String = "/" + reportsFolderName
'设计好的报表 存放的路径
Dim filePath As String = "D:\Project\Reports\"
'链接字符串
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)
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 = "DetechtionCompany"
   
'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) {}
        stream.Read(definition, 
0CInt(stream.Length))
        stream.Close()
    
Catch e As IOException
        Console.WriteLine(e.Message)
    
End Try

    
Try
      
'**********************   parentPath
      If(fileSuffix="rdl"Then
        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 NothingThen
            
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)
    
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+"/DetechtionCompany"
       
Dim dsArray As DataSource()=rs.GetItemDataSources(reportsFolderPath+"/"+reportName)
      
      ds
=dsArray(0)
       ds.Item 
= CType(reference, DataSourceReference)
      rs.SetItemDataSources(reportsFolderPath
+"/"+reportName,dsArray)
     
Catch _exception As Exception
     Console.WriteLine(_exception)
 
End Try
End Sub



3. 新建deploy.dat 批处理文件 运行上面脚本。用到刚刚说的 rs.exe命名 (相应参数上面已经给出)
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 服务器上。。
@author  junmy





posted on 2006-11-27 13:24 record java and net 阅读(1160) 评论(0)  编辑  收藏 所属分类: dot net相关Database


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论