' DTS'未声明 - VB 2010连接字符串作为DB2 SSIS包的变量

时间:2017-04-21 17:00:22

标签: sql-server vb.net ssis db2 dts

完全披露,我处于必须动态学习VB脚本的情况。这是问题: 在VB 2010脚本任务中,我不得不在我的SSIS包中的多个脚本任务中硬编码DB2连接字符串。在VB 2005中,连接字符串被设置为变量,我已经为VB 2010做了。我已经将脚本任务中的变量声明为读/写。我的其他变量有效,但我的DB2和MS SQL连接字符串都需要硬编码才能工作。我看到的错误是"' Dts'没有宣布。"我确信它被声明为代码的其他部分使用正确工作的dts变量。我已经寻找解决方案,但还没有发现任何有效的方法。感谢您提供的任何见解。下面的代码清除敏感信息并替换为星号:

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

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Data.OleDb
Imports System.Data.SqlClient

<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 object. Connections, variables, events,
    ' and logging features are available as static members of the Dts class.
    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
    ' 
    ' To open Code and Text Editor Help, press F1.
    ' To open Object Browser, press Ctrl+Alt+J.
    Dim db2Con As OleDbConnection = Nothing
    Dim strError As String = Nothing
    Dim db2Cmd As OleDbCommand = Nothing
    Dim db2Rdr As OleDbDataReader = Nothing
    Dim EODC_Audit_DataTable As New DataTable

    Public Sub Main()
        '
        ' Add your code here
        '



        Dts.Variables("Audit_Row_Ct").Value = CheckForEODC()
        Dts.TaskResult = ScriptResults.Success


    End Sub

    Public Function CheckForEODC() As Integer
        CheckForEODC = 0
        Try
            If Not DB2RS.SetDBCon(db2Con, strError) Then Throw New Exception(strError)
            If Not DB2RS.SetDBCmd(db2Con, db2Cmd, "SELECT * FROM " + Dts.Variables("Region").Value.ToString + ".****", strError) Then Throw New Exception(strError)
            db2Rdr = db2Cmd.ExecuteReader
            EODC_Audit_DataTable.Load(db2Rdr)
        Catch ex As Exception
            Dts.VariableDispenser.LockForWrite("stdError")
            Dts.Variables("strError").Value = "Load Error:  " & ex.Message & "<br />&nbsp;"
            Dts.Variables.Unlock()
        Finally
            DB2RS.DisposeOfObjects(db2Con, db2Cmd, db2Rdr)
        End Try

        If (**** < 1) Then
            CheckForEODC = 0
        Else
            CheckForEODC = ****
        End If
    End Function
End Class


Public Class DB2RS


    Public Shared Function SetDBCon( _
       ByRef dbCon As OleDbConnection, _
       ByRef strError As String _
   ) As Boolean

        Try
            dbCon = New OleDbConnection(Dts.Variables("DB2ConnectionString").Value.ToString)
            dbCon.Open()
            SetDBCon = True
        Catch ex As Exception
            strError = ex.Message
            SetDBCon = False
        End Try
    End Function

    Public Shared Function SetDBCmd( _
        ByRef dbCon As OleDbConnection, _
        ByRef dbCmd As OleDbCommand, _
        ByVal strCmd As String, _
        ByRef strError As String _
    ) As Boolean

        Try
            dbCmd = New OleDbCommand(strCmd, dbCon)
            dbCmd.CommandType = Data.CommandType.Text
            SetDBCmd = True

        Catch ex As Exception
            strError = ex.Message
            SetDBCmd = False

        End Try
    End Function



    Public Shared Sub DisposeOfObjects( _
        Optional ByRef dbCon As OleDbConnection = Nothing, _
        Optional ByRef dbCmd As OleDbCommand = Nothing, _
        Optional ByRef dbRdr As OleDbDataReader = Nothing, _
        Optional ByRef dbAda As OleDbDataAdapter = Nothing, _
        Optional ByRef dbDSet As DataSet = Nothing, _
        Optional ByRef dTable As DataTable = Nothing, _
        Optional ByRef dView As DataView = Nothing _
    )
        Try
            If Not IsNothing(dbDSet) Then
                dbDSet.Dispose()
                dbDSet = Nothing
            End If
        Catch ex As Exception
            dbDSet = Nothing
        End Try

        Try
            If Not IsNothing(dbAda) Then
                dbAda.Dispose()
                dbAda = Nothing
            End If
        Catch ex As Exception
            dbAda = Nothing
        End Try

        Try
            If Not IsNothing(dbRdr) Then
                If Not dbRdr.IsClosed Then dbRdr.Close()
                dbRdr = Nothing
            End If
        Catch ex As Exception
            dbRdr = Nothing
        End Try
        Try
            If Not IsNothing(dbCmd) Then
                dbCmd.Dispose()
            End If
        Catch ex As Exception
            dbCmd = Nothing
        End Try

        Try
            If Not IsNothing(dbCon) Then
                If dbCon.State = ConnectionState.Open Then
                    dbCon.Close()
                End If
                dbCon.Dispose()
            End If
        Catch ex As Exception
            dbCon = Nothing
        End Try

        Try
            If Not IsNothing(dTable) Then
                dTable.Dispose()
                dTable = Nothing
            End If
        Catch ex As Exception
            dTable = Nothing
        End Try

        Try
            If Not IsNothing(dView) Then
                dView.Dispose()
                dView = Nothing
            End If
        Catch ex As Exception
            dView = Nothing
        End Try
    End Sub

    Public Shared Function Null2Space( _
        ByVal strValue As Object, _
        Optional ByVal blnDash As Boolean = False _
    ) As String

        Try
            If IsNothing(strValue) Then
                If blnDash Then
                    Null2Space = "&nbsp;-&nbsp;"
                Else
                    Null2Space = "&nbsp;"
                End If
            ElseIf strValue Is System.DBNull.Value Then
                If blnDash Then
                    Null2Space = "&nbsp;-&nbsp;"
                Else
                    Null2Space = "&nbsp;"
                End If
            ElseIf strValue.ToString.Trim.Length() < 1 Then
                If blnDash Then
                    Null2Space = "&nbsp;-&nbsp;"
                Else
                    Null2Space = "&nbsp;"
                End If
            Else
                Null2Space = strValue.ToString.Trim()
            End If

        Catch ex As Exception
            Null2Space = strValue.ToString

        End Try
    End Function



End Class











Public Class DBSQLRS

    Public Shared Function SetDBCon( _
        ByRef dbCon As SqlConnection, _
        ByRef strError As String, _
        Optional ByVal dbName As String = "*****", _
        Optional ByVal dbUser As String = "*****" _
    ) As Boolean

        Try
            If dbUser = "ELI_Web" Then
                dbCon = New SqlConnection("*****")
            ElseIf dbUser = "ELI_URP" Then
                dbCon = New SqlConnection("*****")
            End If

            dbCon.Open()
            SetDBCon = True

        Catch ex As Exception
            strError = ex.Message
            SetDBCon = False

        End Try
    End Function

    Public Shared Function SetDBCmd( _
        ByRef dbCon As SqlConnection, _
        ByRef dbCmd As SqlCommand, _
        ByVal strCmd As String, _
        ByRef strError As String _
    ) As Boolean

        Try
            dbCmd = New SqlCommand(strCmd, dbCon)
            dbCmd.CommandType = Data.CommandType.StoredProcedure
            SetDBCmd = True

        Catch ex As Exception
            strError = ex.Message
            SetDBCmd = False

        End Try
    End Function

    Public Shared Function SetDBPar( _
        ByRef dbCmd As SqlCommand, _
        ByRef dbPar As SqlParameter, _
        ByVal strParName As String, _
        ByVal objParValue As Object, _
        ByVal dbType As Data.SqlDbType, _
        ByRef strError As String _
    ) As Boolean

        Try
            dbPar = New SqlParameter(strParName, dbType)
            dbPar.IsNullable = True
            dbPar.Value = objParValue
            dbCmd.Parameters.Add(dbPar)
            SetDBPar = True

        Catch ex As Exception
            strError = ex.Message
            SetDBPar = False

        End Try
    End Function

    Public Shared Sub DisposeOfObjects( _
        Optional ByRef dbCon As SqlConnection = Nothing, _
        Optional ByRef dbCmd As SqlCommand = Nothing, _
        Optional ByRef dbPar As SqlParameter = Nothing, _
        Optional ByRef dbRdr As SqlDataReader = Nothing, _
        Optional ByRef dbAda As SqlDataAdapter = Nothing, _
        Optional ByRef dbDSet As DataSet = Nothing, _
        Optional ByRef dTable As DataTable = Nothing, _
        Optional ByRef dView As DataView = Nothing _
    )
        Try
            If Not IsNothing(dbDSet) Then
                dbDSet.Dispose()
                dbDSet = Nothing
            End If
        Catch ex As Exception
            dbDSet = Nothing
        End Try

        Try
            If Not IsNothing(dbAda) Then
                dbAda.Dispose()
                dbAda = Nothing
            End If
        Catch ex As Exception
            dbAda = Nothing
        End Try

        Try
            If Not IsNothing(dbRdr) Then
                If Not dbRdr.IsClosed Then dbRdr.Close()
                dbRdr = Nothing
            End If
        Catch ex As Exception
            dbRdr = Nothing
        End Try

        Try
            If Not IsNothing(dbPar) Then
                dbPar = Nothing
            End If
        Catch ex As Exception
            dbPar = Nothing
        End Try

        Try
            If Not IsNothing(dbCmd) Then
                dbCmd.Dispose()
            End If
        Catch ex As Exception
            dbCmd = Nothing
        End Try

        Try
            If Not IsNothing(dbCon) Then
                If dbCon.State = ConnectionState.Open Then
                    dbCon.Close()
                End If
                dbCon.Dispose()
            End If
        Catch ex As Exception
            dbCon = Nothing
        End Try

        Try
            If Not IsNothing(dTable) Then
                dTable.Dispose()
                dTable = Nothing
            End If
        Catch ex As Exception
            dTable = Nothing
        End Try

        Try
            If Not IsNothing(dView) Then
                dView.Dispose()
                dView = Nothing
            End If
        Catch ex As Exception
            dView = Nothing
        End Try
    End Sub

    Public Shared Function Null2Space( _
        ByVal strValue As Object, _
        Optional ByVal blnDash As Boolean = False _
    ) As String

        Try
            If IsNothing(strValue) Then
                If blnDash Then
                    Null2Space = "&nbsp;-&nbsp;"
                Else
                    Null2Space = "&nbsp;"
                End If
            ElseIf strValue Is System.DBNull.Value Then
                If blnDash Then
                    Null2Space = "&nbsp;-&nbsp;"
                Else
                    Null2Space = "&nbsp;"
                End If
            ElseIf strValue.ToString.Trim.Length() < 1 Then
                If blnDash Then
                    Null2Space = "&nbsp;-&nbsp;"
                Else
                    Null2Space = "&nbsp;"
                End If
            Else
                Null2Space = strValue.ToString.Trim()
            End If

        Catch ex As Exception
            Null2Space = strValue.ToString

        End Try
    End Function

End Class

1 个答案:

答案 0 :(得分:0)

看起来.horizontal-navigation-bar nav ul li.active-link a:after { background: none !important; } Dts类中的实例属性(继承自ScriptMain)。

但是,您的Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectM‌​odelBase调用位于名为OleDbConnection()的其他类中,该类无法访问DB2RS类的实例变量或属性,例如ScriptMain

如果您不明白这一点,那么您应该退后一步并参加一个初学者Visual Basic编程课程,因为即使下一个解决方案可以帮助您,您也不会进一步学习。这是一些资源

解决当前问题的一种方法是将Dts作为参数传递给Dts来电。改变这一行:

DB2RS.SetDBCon()

为:

If Not DB2RS.SetDBCon(db2Con, strError) Then Throw New Exception(strError)

然后,您需要将If Not DB2RS.SetDBCon(db2Con, Dts, strError) Then Throw New Exception(strError) 作为参数添加到Dts函数中。我认为SetDBCon()的类型是Dts,因此我将其包含在下方。如果这是错的,你需要调整那种类型..

Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel