我想使用SSIS导出PDF文件。要导出的报告有一个参数(type = integer)。我在互联网上找到了完全符合here的代码。
但是,当我从ssis运行此代码时,它会导出pdf,但文件大小为0KB。尝试打开pdf文件时出错。
以下是我正在使用的代码:
Imports System
Imports System.Data
Imports System.Net
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.ComponentModel
Imports System.Diagnostics
<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)>
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Protected Sub SaveFile(ByVal url As String, ByVal localpath As String)
Dim loRequest As System.Net.HttpWebRequest
Dim myCredentials As New NetworkCredential("username", "password")
Dim loResponse As System.Net.HttpWebResponse
Dim loResponseStream As System.IO.Stream
Dim loFileStream As New System.IO.FileStream(localpath, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim laBytes(256) As Byte
Dim liCount As Integer = 1
Try
loRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
loRequest.Credentials = myCredentials
loRequest.Timeout = 600000
loRequest.Method = "GET"
loResponse = CType(loRequest.GetResponse, System.Net.HttpWebResponse)
loResponseStream = loResponse.GetResponseStream
Do While liCount > 0
liCount = loResponseStream.Read(laBytes, 0, 256)
loFileStream.Write(laBytes, 0, liCount)
Loop
loFileStream.Flush()
loFileStream.Close()
Catch ex As Exception
End Try
End Sub
Public Sub Main()
Dim url, destination As String
destination = Dts.Variables("varDestinationPath").Value.ToString + "\" + Dts.Variables("varRSParameter").Value.ToString + " " + Format(Now, "yyyyMMdd") + ".pdf"
url = "http://Server/ReportServer/Pages/ReportViewer.aspx?%2fMap%2fMap%2fMap%2Repportname&Parameter=" + Dts.Variables("varRSParameter").Value.ToString + "&rs:Format=PDF"
SaveFile(url, destination)
Dts.TaskResult = ScriptResults.Success
End Sub
我试图转到网址http://Server/ReportServer/Pages/ReportViewer.aspx?%2fMapName%2fMapName%2fMapName%2Repportname&Parameter=&#34; parametersqlresults&#34; Format = PDF
当我这样做时,我会下载pdf,一切正常。
我认为可能存在与报表服务器连接的内容,因此我将其添加到代码中:
`Imports System.Net
Dim myCredentials As New NetworkCredential("username", "password")
loRequest.Credentials = myCredentials`
同样在Windows日志中的报告服务器上&gt;安全 我可以使用提供的用户名和密码查看审计成功。
我将位置更改为pdf保存到多个位置的位置。我还从数据类型&#39; String&#39;中更改了变量数据类型。到&#39; INT32&#39; 没有成功
答案 0 :(得分:1)
我在几个月前发生过这个问题,修好了它,忘记了它,当我们进行服务器升级时,再次发生这种情况只是为了弄清楚如何修复它而撞到了墙上。 / p>
问题是当您运行代码时,您正在以自己的身份运行,并且可能在SSRS网站上拥有“浏览器”(或更好)权限。当您将其作为SQL作业运行时,作业将运行SQL代理帐户或代理帐户。您还需要在SSRS端授予该SQL帐户这些浏览器权限。
希望有所帮助, 瑞安戴维森