【问题标题】:DTS is not declared SSIS 2008 VB.Net script taskDTS 未声明 SSIS 2008 VB.Net 脚本任务
【发布时间】:2021-07-20 04:31:07
【问题描述】:

我有下面的脚本,它将获取一个 .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

【问题讨论】:

    标签: vbscript ssis


    【解决方案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
    

    【讨论】:

    • 没问题,欢迎来到 SO。如果您发现答案有帮助,请随时点击向上箭头,如果这解决了您的问题,请点击绿色复选标记。
    • 投反对票,(嗨@TheOptimusPrime)你能帮我理解投反对票以便我改进这个答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多