将 DTS ActiveX 转换为 SSIS 中的脚本任务

Convert DTS ActiveX to Script Task in SSIS

我是这个论坛的新手,这是我的第一个帖子,所以我希望我把它发布在正确的位置。

我不知道任何 C#,但我知道一些 VB,我已将我的 DTS 程序包迁移到 SSIS,但我无法使 ActiveX 脚本工作并决定将其重写为脚本任务。我有 4 个全局变量,它们都已在全局变量菜单中设置了值。 link 给一张图片:Link。那里有 ActiveX 代码,因此用户在切换 server/database 时只需更改 2 个变量,它仍然会在那里的表上执行相同的操作(所有表在所有服务器和数据库中都具有相同的名称,因此不会需要调整)并且每天自动 运行。这是原始的 ActiveX 代码:

Function Main()

Dim sEnvironm       
Dim sServer            
Dim sSourceFile     
Dim sSourcePath    
Dim sBackupPath  
Dim sErrorPath      
Dim sFileName        
Dim sUDLPath    

'*********************************************************
' Set vars 
' 
' First 2 are depending on the server and db
' FILL IN THE RIGHT VALUES
' 
'*********************************************************
sEnvironm = "MON_Datamart"
sServer =  "W0254"



' --- DO NOT CHANGE ANYTHING BELOW THIS LINE ---
' ---
sSourceFile  = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\SourceFiles\tbl_L47T1.txt"
sSourcePath = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\SourceFiles\"
sBackupPath  = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\BackupFiles\"
sErrorPath   = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\ErrorFiles\"
sFileName = "tbl_L47T1.txt"
sUDLPath = "\" & sServer & "\Data_sql\" & sEnvironm & "\UDL\" & sEnvironm & ".udl"


FoundError = False

Set oPackage = DTSGlobalVariables.Parent

Set oConnection = oPackage.Connections("tbl_L47T1.txt")
oConnection.DataSource = sSourceFile

Set oConnection = oPackage.Connections("Datamart")
oConnection.UDLPath = sUDLPath 

Set oConnection = oPackage.Connections("Truncate")
oConnection.UDLPath = sUDLPath 

Set oTask = oPackage.Tasks("DTSTask_DTSDataPumpTask_1").CustomTask
oTask.SourceObjectName = sSourceFile
oTask.DestinationObjectName =  sEnvironm & ".dbo.stg_tbl_L47T1" 

DTSGlobalVariables("SourcePath").Value = sSourceFile
DTSGlobalVariables("BackupPath").Value = sBackupPath
DTSGlobalVariables("ErrorPath").Value = sErrorPath
DTSGlobalVariables("FileName").Value = sFileName   

Set oTask = Nothing
Set oConnection = Nothing
Set oPackage = Nothing

Main = DTSTaskExecResult_Success

End Function 

这是我目前所拥有的:

Public Sub Main()

        Dim sEnvironm As  String
        Dim sServer As String
        Dim sSourceFile As String
        Dim sSourcePath As String
        Dim sBackupPath As String
        Dim sErrorPath As String
        Dim sFileName As String
        Dim sUDLPath As String
        Dim FoundError As Boolean
        Dim oPackage As Object
        Dim oConnection As Object
        Dim oTask As Object
        Dim DTSGlobalVariables As Object



        '*********************************************************
        ' Set vars 
        ' 
        ' First 2 are depending on the server and db
        ' FILL IN THE RIGHT VALUES
        ' 
        '*********************************************************
        sEnvironm = "MON_Datamart"
sServer =  "W0254"



' --- DO NOT CHANGE ANYTHING BELOW THIS LINE ---
' ---
sSourceFile  = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\SourceFiles\tbl_L47T1.txt"
sSourcePath = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\SourceFiles\"
sBackupPath  = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\BackupFiles\"
sErrorPath   = "\" & sServer & "\Data_sql\" & sEnvironm & "\DTS\ErrorFiles\"
sFileName = "tbl_L47T1.txt"
sUDLPath = "\" & sServer & "\Data_sql\" & sEnvironm & "\UDL\" & sEnvironm & ".udl"

FoundError = False

oPackage = Dts.Variables("User::VariableName").Value

oConnection = oPackage.Connections("tbl_L47T1.txt")
oConnection.DataSource = sSourceFile

oConnection = oPackage.Connections("Datamart")
oConnection.UDLPath = sUDLPath 

oConnection = oPackage.Connections("Truncate")
oConnection.UDLPath = sUDLPath 

oTask = oPackage.Tasks("DTSTask_DTSDataPumpTask_1").CustomTask
oTask.SourceObjectName = sSourceFile
oTask.DestinationObjectName =  sEnvironm & ".dbo.stg_tbl_L47T1" 

DTSGlobalVariables("SourcePath").Value = sSourceFile
DTSGlobalVariables("BackupPath").Value = sBackupPath
DTSGlobalVariables("ErrorPath").Value = sErrorPath
DTSGlobalVariables("FileName").Value = sFileName   

oTask = Nothing
oConnection = Nothing
oPackage = Nothing

Main = DTSTaskExecResult_Success
    End Sub 

如果我调试它,我会遇到很多错误,而且我不知道如何修复它们...

错误:

Error: System.Reflection.TargetInvocationException: Het doel van een aanroep heeft een uitzondering veroorzaakt. ---> Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException: The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.
 ---> System.Runtime.InteropServices.COMException (0xC0010009): The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.

   bij Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSVariables100.get_Item(Object Index)
   bij Microsoft.SqlServer.Dts.Runtime.Variables.get_Item(Object index)
   --- Einde van intern uitzonderingsstackpad ---
   bij Microsoft.SqlServer.Dts.Runtime.Variables.get_Item(Object index)
   bij ST_f32fc12b60f34bebbbdfc0c5e5b40a96.vbproj.ScriptMain.Main()
   --- Einde van intern uitzonderingsstackpad ---
   bij System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   bij System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   bij System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   bij System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   bij Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

有人可以帮我进一步转换吗?我被卡住了,不知道如何解决我的错误...

提前致谢!

我建议您完全摆脱您的脚本,而是使用包配置来设置源服务器。然后你只需编辑一个 XML 文件,而不是实际的包。

这里有一个很好的指南:https://www.simple-talk.com/sql/ssis/xml-configuration-files-in-sql-server-integration-services/

记住一点:不要编辑连接管理器的逻辑名称,而是编辑其中的连接字符串。

我建议您将连接管理器重命名为它们的真实名称(即 "customersystem" 如果从客户系统中提取客户)而不是它们指向的服务器 (MySQLServerHost\SQLExpress.DBName),因为实际的连接详细信息会因您的配置而异。