2017-04-21 45 views
0

充分披露,我在我在學習上飛VB腳本的情況。這裏是問題: 在2010腳本任務VB,我不得不硬編碼在我的SSIS包多個腳本任務的DB2連接字符串。在VB 2005中,連接字符串被設置爲一個變量,這是我爲VB 2010所做的。我已經將腳本任務中的變量聲明爲讀/寫。我的其他變量可以工作,但我的DB2和MS SQL連接字符串都需要硬編碼才能工作。我看到的錯誤是「'Dts'沒有被聲明。」我確定它被聲明爲代碼的其他部分使用dts變量可以正常工作。我已經搜索瞭解決方案,但還沒有找到任何可行的方法。感謝您提供的任何見解。下面的代碼與擦洗敏感信息,並用星號代替:「DTS」未聲明 - VB 2010連接字符串爲變量到DB2 SSIS包

' 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 
+0

更具體地講,在出現錯誤:dbCon =新的OleDbConnection(Dts.Variables(「DB2ConnectionString」)Value.ToString – BrentP

回答

0

看起來Dts在你ScriptMain類(從Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectM‌​odelBase繼承)的實例屬性。

但是,您的OleDbConnection()調用位於不同的類中,稱爲DB2RS,該類不能訪問實例變量或ScriptMain類的屬性,如Dts

如果你不明白,你應該退後一步,並採取初學者Visual Basic編程類,因爲你不會得到哪怕更遠在下一個解決方案可以幫助你。這裏有一些資源

一種方式是通過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,所以我將它包含在下面。如果說的不對,你需要調整型..

Public Class DB2RS 
    Public Shared Function SetDBCon(_ 
     ByRef dbCon As OleDbConnection, _ 
     ByRef Dts As Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel, _ 
     ByRef strError As String _ 
    ) As Boolean 
+0

老實說,我不知道該怎麼做。我最感動的代碼從2005年到VS 2010 VS我通過爲了得到它的功能調試代碼所作的調整,但它需要硬編碼的所有連接字符串。我需要他們是動態的變量。我很抱歉,但正如我所說,我是很新的這一點,並盡我所能去學習如何做到這一點儘可能快 – BrentP

+0

你會從更多地瞭解Visual Basic編程中受益。我建議初學者VB編程書籍和/或在線課程,例如 - https://mva.microsoft.com/en-US/training-courses/visual-basic-fundament ALS換絕對初學者-16507?L = jqMOvLKbC_9206218965 –

+0

感謝所有幫助。我一直在尋找你提供的關於VB的更多的鏈接。正如我所說,這真的不是我的角色的一部分,但這是需要做的事情,這裏沒有其他人熟悉VB。這絕對是我正在嘗試提高的技能。 – BrentP