【问题标题】:SSIS Script Task not working in Visual Studio 2010, Exception has been thrown by the target of an invocationSSIS 脚本任务在 Visual Studio 2010 中不起作用,调用目标已引发异常
【发布时间】:2018-11-07 21:29:39
【问题描述】:

我正在使用 SSIS 脚本任务,但每当我运行它时,SSIS 包都会失败,并给出以下错误:调用目标已引发异常。

是否有可能出现此问题,因为我在 Visual Studio 2008 中使用此脚本并且我正在尝试在 Visual Studio 2010 中实现该包。

这是我的代码:

  enter code here ' Microsoft SQL Server Integration Services Script Task
 ' Write scripts using Microsoft Visual Basic 2008.
 ' The ScriptMain is the entry point class of the script.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO

<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute> _
<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


' The execution engine calls this method when the task executes.
' To access the object model, use the Dts property. Connections, variables, events,
' and logging features are available as members of the Dts property as shown in the following examples.
'
' To reference a variable, call Dts.Variables("MyCaseSensitiveVariableName").Value
' To post a log entry, call Dts.Log("This is my log text", 999, Nothing)
' To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, True)
'
' To use the connections collection use something like the following:
' ConnectionManager cm = Dts.Connections.Add("OLEDB")
' cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"
'
' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
' 
' To open Help, press F1.

Public Sub Main()
    Dim file_stream As New FileStream(CType(ReadVariable("filepath"), String) + "AIR_FEE1.TRN", FileMode.Append)
    Using w As New StreamWriter(file_stream, Text.Encoding.UTF8)
        w.WriteLine("T|" + CType(ReadVariable("Count"), String))
    End Using
    Dim FName As String
    Dim LFName As String
    FName = CType(ReadVariable("filename"), String)
    LFName = CType(ReadVariable("logfile"), String)
    WriteVariable("StaticLogFileName", LFName)
    WriteVariable("StaticFileName", FName)
    Dim file_stream1 As New FileStream("StaticFileName", FileMode.Create)
    file_stream.Close()
    Dts.TaskResult = ScriptResults.Success
End Sub
Private Function ReadVariable(ByVal varName As String) As Object
    Dim result As Object
    Try
        Dim vars As Variables
        Dts.VariableDispenser.LockForRead(varName)
        Dts.VariableDispenser.GetVariables(vars)
        Try
            result = vars(varName).Value
        Catch ex As Exception
            Throw ex
        Finally
            vars.Unlock()
        End Try
    Catch ex As Exception
        Throw ex
    End Try
    Return result
End Function

Private Sub WriteVariable(ByVal varName As String, ByVal varValue As Object)
    Try
        Dim vars As Variables
        Dts.VariableDispenser.LockForWrite(varName)
        Dts.VariableDispenser.GetVariables(vars)
        Try
            vars(varName).Value = varValue
        Catch ex As Exception
            Throw ex
        Finally
            vars.Unlock()
        End Try
    Catch ex As Exception
        Throw ex
    End Try
End Sub

End Class

【问题讨论】:

  • 尝试在您的脚本任务中添加一个 try/catch 块并捕获它,以便将其输入到包中,您可以使用它来做一些事情(或者在调试过程中最坏的情况下,如果出现错误,请使用消息框您只需要在 UI 中运行 /testing 时进行调试

标签: sql-server vb.net visual-studio ssis etl


【解决方案1】:

首先,"Exception has been throwed by the target of an invocation"是一个通用消息,当脚本任务执行过程中发生错误时抛出,尝试调试你的代码找到一个更准确的错误信息。

我认为您可以编写相同的脚本而无需定义函数来操作您的变量:

Public Sub Main()

    Dim file_stream As New FileStream(Dts.Variables("filepath").Value + "AIR_FEE1.TRN", FileMode.Append)

    Using w As New StreamWriter(file_stream, Text.Encoding.UTF8)
        w.WriteLine("T|" + Dts.Variables("Count").Value)
    End Using

    Dim FName As String
    Dim LFName As String

    FName = Dts.Variables("filename").Value
    LFName = Dts.Variables("logfile").Value

    Dts.Variables("StaticLogFileName").Value =  LFName
    Dts.Variables("StaticFileName").Value =  FName

    Dim file_stream1 As New FileStream("StaticFileName", FileMode.Create)
    file_stream.Close()


    Dts.TaskResult = ScriptResults.Success

End Sub

确保您已从脚本任务属性表单中正确选择了 ReadOnly VariablesReadWrite Variables

有用的链接

【讨论】:

    猜你喜欢
    • 2018-09-20
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    相关资源
    最近更新 更多