Ladies and Gents,我有下面的脚本,它将获取.gz文件并解压缩。但是,每当我尝试使用此代码时,我都无法验证脚本。
错误消息 - 未声明名称Dts。
有人能帮忙解决这个问题吗?我很少在SSIS中使用脚本任务。
Imports System
Imports System.IO
Imports System.Data
Imports System.Math
Imports System.Text
Imports System.IO.Compression
Imports Microsoft.SqlServer.Dts.Runtime
'''<summary>Decompress file</summary>
Public Class ScriptMain
Public Sub Main()
Dts.TaskResult = Dts.Results.Success
Dim success As Boolean = True
Dim workFilePath As String
workFilePath = Dts.Variables("User::sBloombergReplyFileFullPathUR0L").Value.ToString()
If File.Exists(workFilePath) Then
If Not workFilePath.EndsWith(".gz") Then
Dts.Events.FireInformation(0, "", workFilePath + " is not compressed; skipping decompression", Nothing, -1, True)
Return
End If
Dim uncompressedFileName As String
Dim bytes(Int16.MaxValue) As Byte
Dim n As Integer = 1
Try
uncompressedFileName = workFilePath.Substring(0, workFilePath.Length - 3)
Dts.Events.FireInformation(0, "", "decompressing " + workFilePath + " to " + uncompressedFileName, Nothing, -1, True)
Using writer As New FileStream(uncompressedFileName, FileMode.Create)
Using compressedStream As Stream = File.Open(workFilePath, FileMode.Open, FileAccess.Read, FileShare.None)
Using unzipper As New GZipStream(compressedStream, CompressionMode.Decompress)
Do Until n = 0
n = unzipper.Read(bytes, 0, bytes.Length)
writer.Write(bytes, 0, n)
Loop
unzipper.Close()
End Using
compressedStream.Close()
End Using
writer.Close()
End Using
Catch ex As Exception
Dts.Events.FireError(0, ex.TargetSite().ToString(), "Unable to decompress " + workFilePath + "; " + ex.Message, Nothing, -1)
success = False
Finally
If success = False And File.Exists(uncompressedFileName) Then
Dts.TaskResult = Dts.Results.Failure
File.Delete(uncompressedFileName)
End If
End Try
Else
Dts.Events.FireError(0, "", workFilePath + " does not exist", Nothing, -1)
Dts.TaskResult = Dts.Results.Failure
Return
End If
End Sub
End Class
答案 0 :(得分:1)
你班上的Dts是未知的。由于ScriptMain
不再延伸Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Imports System
Imports System.IO
Imports System.Data
Imports System.Math
Imports System.Text
Imports System.IO.Compression
Imports Microsoft.SqlServer.Dts.Runtime
<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Public Sub Main()
Dts.TaskResult = Dts.Results.Success
Dim success As Boolean = True
Dim workFilePath As String
workFilePath = Dts.Variables("User::sBloombergReplyFileFullPathUR0L").Value.ToString()
If File.Exists(workFilePath) Then
If Not workFilePath.EndsWith(".gz") Then
Dts.Events.FireInformation(0, "", workFilePath + " is not compressed; skipping decompression", Nothing, -1, True)
Return
End If
Dim uncompressedFileName As String
Dim bytes(Int16.MaxValue) As Byte
Dim n As Integer = 1
Try
uncompressedFileName = workFilePath.Substring(0, workFilePath.Length - 3)
Dts.Events.FireInformation(0, "", "decompressing " + workFilePath + " to " + uncompressedFileName, Nothing, -1, True)
Using writer As New FileStream(uncompressedFileName, FileMode.Create)
Using compressedStream As Stream = File.Open(workFilePath, FileMode.Open, FileAccess.Read, FileShare.None)
Using unzipper As New GZipStream(compressedStream, CompressionMode.Decompress)
Do Until n = 0
n = unzipper.Read(bytes, 0, bytes.Length)
writer.Write(bytes, 0, n)
Loop
unzipper.Close()
End Using
compressedStream.Close()
End Using
writer.Close()
End Using
Catch ex As Exception
Dts.Events.FireError(0, ex.TargetSite().ToString(), "Unable to decompress " + workFilePath + "; " + ex.Message, Nothing, -1)
success = False
Finally
If success = False And File.Exists(uncompressedFileName) Then
Dts.TaskResult = Dts.Results.Failure
File.Delete(uncompressedFileName)
End If
End Try
Else
Dts.Events.FireError(0, "", workFilePath + " does not exist", Nothing, -1)
Dts.TaskResult = Dts.Results.Failure
Return
End If
End Sub
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
End Class