Advertisement
Guest User

VB 2010 script task code

a guest
Apr 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Microsoft SQL Server Integration Services Script Task
  2. ' Write scripts using Microsoft Visual Basic
  3. ' The ScriptMain class is the entry point of the Script Task.
  4.  
  5. Imports System
  6. Imports System.Data
  7. Imports System.Math
  8. Imports Microsoft.SqlServer.Dts.Runtime
  9. Imports System.Data.OleDb
  10. Imports System.Data.SqlClient
  11.  
  12. <Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute> _
  13. <System.CLSCompliantAttribute(False)> _
  14. Partial Public Class ScriptMain
  15.     Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
  16.  
  17.     Enum ScriptResults
  18.         Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
  19.         Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  20.     End Enum
  21.  
  22.     ' The execution engine calls this method when the task executes.
  23.    ' To access the object model, use the Dts object. Connections, variables, events,
  24.    ' and logging features are available as static members of the Dts class.
  25.    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
  26.    '
  27.    ' To open Code and Text Editor Help, press F1.
  28.    ' To open Object Browser, press Ctrl+Alt+J.
  29.    Dim db2Con As OleDbConnection = Nothing
  30.     Dim strError As String = Nothing
  31.     Dim db2Cmd As OleDbCommand = Nothing
  32.     Dim db2Rdr As OleDbDataReader = Nothing
  33.     Dim EODC_Audit_DataTable As New DataTable
  34.  
  35.     Public Sub Main()
  36.         '
  37.        ' Add your code here
  38.        '
  39.  
  40.  
  41.  
  42.         Dts.Variables("Audit_Row_Ct").Value = CheckForEODC()
  43.         Dts.TaskResult = ScriptResults.Success
  44.  
  45.  
  46.     End Sub
  47.  
  48.     Public Function CheckForEODC() As Integer
  49.         CheckForEODC = 0
  50.         Try
  51.             If Not DB2RS.SetDBCon(db2Con, strError) Then Throw New Exception(strError)
  52.             If Not DB2RS.SetDBCmd(db2Con, db2Cmd, "SELECT * FROM " + Dts.Variables("Region").Value.ToString + ".****", strError) Then Throw New Exception(strError)
  53.             db2Rdr = db2Cmd.ExecuteReader
  54.             EODC_Audit_DataTable.Load(db2Rdr)
  55.         Catch ex As Exception
  56.             Dts.VariableDispenser.LockForWrite("stdError")
  57.             Dts.Variables("strError").Value = "Load Error:  " & ex.Message & "<br />&nbsp;"
  58.             Dts.Variables.Unlock()
  59.         Finally
  60.             DB2RS.DisposeOfObjects(db2Con, db2Cmd, db2Rdr)
  61.         End Try
  62.  
  63.         If (**** < 1) Then
  64.             CheckForEODC = 0
  65.         Else
  66.             CheckForEODC = ****
  67.         End If
  68.     End Function
  69. End Class
  70.  
  71.  
  72. Public Class DB2RS
  73.  
  74.  
  75.     Public Shared Function SetDBCon( _
  76.        ByRef dbCon As OleDbConnection, _
  77.        ByRef strError As String _
  78.    ) As Boolean
  79.  
  80.         Try
  81.             dbCon = New OleDbConnection(Dts.Variables("DB2ConnectionString").Value.ToString)
  82.             dbCon.Open()
  83.             SetDBCon = True
  84.         Catch ex As Exception
  85.             strError = ex.Message
  86.             SetDBCon = False
  87.         End Try
  88.     End Function
  89.  
  90.     Public Shared Function SetDBCmd( _
  91.         ByRef dbCon As OleDbConnection, _
  92.         ByRef dbCmd As OleDbCommand, _
  93.         ByVal strCmd As String, _
  94.         ByRef strError As String _
  95.     ) As Boolean
  96.  
  97.         Try
  98.             dbCmd = New OleDbCommand(strCmd, dbCon)
  99.             dbCmd.CommandType = Data.CommandType.Text
  100.             SetDBCmd = True
  101.  
  102.         Catch ex As Exception
  103.             strError = ex.Message
  104.             SetDBCmd = False
  105.  
  106.         End Try
  107.     End Function
  108.  
  109.  
  110.  
  111.     Public Shared Sub DisposeOfObjects( _
  112.         Optional ByRef dbCon As OleDbConnection = Nothing, _
  113.         Optional ByRef dbCmd As OleDbCommand = Nothing, _
  114.         Optional ByRef dbRdr As OleDbDataReader = Nothing, _
  115.         Optional ByRef dbAda As OleDbDataAdapter = Nothing, _
  116.         Optional ByRef dbDSet As DataSet = Nothing, _
  117.         Optional ByRef dTable As DataTable = Nothing, _
  118.         Optional ByRef dView As DataView = Nothing _
  119.     )
  120.         Try
  121.             If Not IsNothing(dbDSet) Then
  122.                 dbDSet.Dispose()
  123.                 dbDSet = Nothing
  124.             End If
  125.         Catch ex As Exception
  126.             dbDSet = Nothing
  127.         End Try
  128.  
  129.         Try
  130.             If Not IsNothing(dbAda) Then
  131.                 dbAda.Dispose()
  132.                 dbAda = Nothing
  133.             End If
  134.         Catch ex As Exception
  135.             dbAda = Nothing
  136.         End Try
  137.  
  138.         Try
  139.             If Not IsNothing(dbRdr) Then
  140.                 If Not dbRdr.IsClosed Then dbRdr.Close()
  141.                 dbRdr = Nothing
  142.             End If
  143.         Catch ex As Exception
  144.             dbRdr = Nothing
  145.         End Try
  146.         Try
  147.             If Not IsNothing(dbCmd) Then
  148.                 dbCmd.Dispose()
  149.             End If
  150.         Catch ex As Exception
  151.             dbCmd = Nothing
  152.         End Try
  153.  
  154.         Try
  155.             If Not IsNothing(dbCon) Then
  156.                 If dbCon.State = ConnectionState.Open Then
  157.                     dbCon.Close()
  158.                 End If
  159.                 dbCon.Dispose()
  160.             End If
  161.         Catch ex As Exception
  162.             dbCon = Nothing
  163.         End Try
  164.  
  165.         Try
  166.             If Not IsNothing(dTable) Then
  167.                 dTable.Dispose()
  168.                 dTable = Nothing
  169.             End If
  170.         Catch ex As Exception
  171.             dTable = Nothing
  172.         End Try
  173.  
  174.         Try
  175.             If Not IsNothing(dView) Then
  176.                 dView.Dispose()
  177.                 dView = Nothing
  178.             End If
  179.         Catch ex As Exception
  180.             dView = Nothing
  181.         End Try
  182.     End Sub
  183.  
  184.     Public Shared Function Null2Space( _
  185.         ByVal strValue As Object, _
  186.         Optional ByVal blnDash As Boolean = False _
  187.     ) As String
  188.  
  189.         Try
  190.             If IsNothing(strValue) Then
  191.                 If blnDash Then
  192.                     Null2Space = "&nbsp;-&nbsp;"
  193.                 Else
  194.                     Null2Space = "&nbsp;"
  195.                 End If
  196.             ElseIf strValue Is System.DBNull.Value Then
  197.                 If blnDash Then
  198.                     Null2Space = "&nbsp;-&nbsp;"
  199.                 Else
  200.                     Null2Space = "&nbsp;"
  201.                 End If
  202.             ElseIf strValue.ToString.Trim.Length() < 1 Then
  203.                 If blnDash Then
  204.                     Null2Space = "&nbsp;-&nbsp;"
  205.                 Else
  206.                     Null2Space = "&nbsp;"
  207.                 End If
  208.             Else
  209.                 Null2Space = strValue.ToString.Trim()
  210.             End If
  211.  
  212.         Catch ex As Exception
  213.             Null2Space = strValue.ToString
  214.  
  215.         End Try
  216.     End Function
  217.  
  218.  
  219.  
  220. End Class
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. Public Class DBSQLRS
  233.  
  234.     Public Shared Function SetDBCon( _
  235.         ByRef dbCon As SqlConnection, _
  236.         ByRef strError As String, _
  237.         Optional ByVal dbName As String = "*****", _
  238.         Optional ByVal dbUser As String = "*****" _
  239.     ) As Boolean
  240.  
  241.         Try
  242.             If dbUser = "ELI_Web" Then
  243.                 dbCon = New SqlConnection("*****")
  244.             ElseIf dbUser = "ELI_URP" Then
  245.                 dbCon = New SqlConnection("*****")
  246.             End If
  247.  
  248.             dbCon.Open()
  249.             SetDBCon = True
  250.  
  251.         Catch ex As Exception
  252.             strError = ex.Message
  253.             SetDBCon = False
  254.  
  255.         End Try
  256.     End Function
  257.  
  258.     Public Shared Function SetDBCmd( _
  259.         ByRef dbCon As SqlConnection, _
  260.         ByRef dbCmd As SqlCommand, _
  261.         ByVal strCmd As String, _
  262.         ByRef strError As String _
  263.     ) As Boolean
  264.  
  265.         Try
  266.             dbCmd = New SqlCommand(strCmd, dbCon)
  267.             dbCmd.CommandType = Data.CommandType.StoredProcedure
  268.             SetDBCmd = True
  269.  
  270.         Catch ex As Exception
  271.             strError = ex.Message
  272.             SetDBCmd = False
  273.  
  274.         End Try
  275.     End Function
  276.  
  277.     Public Shared Function SetDBPar( _
  278.         ByRef dbCmd As SqlCommand, _
  279.         ByRef dbPar As SqlParameter, _
  280.         ByVal strParName As String, _
  281.         ByVal objParValue As Object, _
  282.         ByVal dbType As Data.SqlDbType, _
  283.         ByRef strError As String _
  284.     ) As Boolean
  285.  
  286.         Try
  287.             dbPar = New SqlParameter(strParName, dbType)
  288.             dbPar.IsNullable = True
  289.             dbPar.Value = objParValue
  290.             dbCmd.Parameters.Add(dbPar)
  291.             SetDBPar = True
  292.  
  293.         Catch ex As Exception
  294.             strError = ex.Message
  295.             SetDBPar = False
  296.  
  297.         End Try
  298.     End Function
  299.  
  300.     Public Shared Sub DisposeOfObjects( _
  301.         Optional ByRef dbCon As SqlConnection = Nothing, _
  302.         Optional ByRef dbCmd As SqlCommand = Nothing, _
  303.         Optional ByRef dbPar As SqlParameter = Nothing, _
  304.         Optional ByRef dbRdr As SqlDataReader = Nothing, _
  305.         Optional ByRef dbAda As SqlDataAdapter = Nothing, _
  306.         Optional ByRef dbDSet As DataSet = Nothing, _
  307.         Optional ByRef dTable As DataTable = Nothing, _
  308.         Optional ByRef dView As DataView = Nothing _
  309.     )
  310.         Try
  311.             If Not IsNothing(dbDSet) Then
  312.                 dbDSet.Dispose()
  313.                 dbDSet = Nothing
  314.             End If
  315.         Catch ex As Exception
  316.             dbDSet = Nothing
  317.         End Try
  318.  
  319.         Try
  320.             If Not IsNothing(dbAda) Then
  321.                 dbAda.Dispose()
  322.                 dbAda = Nothing
  323.             End If
  324.         Catch ex As Exception
  325.             dbAda = Nothing
  326.         End Try
  327.  
  328.         Try
  329.             If Not IsNothing(dbRdr) Then
  330.                 If Not dbRdr.IsClosed Then dbRdr.Close()
  331.                 dbRdr = Nothing
  332.             End If
  333.         Catch ex As Exception
  334.             dbRdr = Nothing
  335.         End Try
  336.  
  337.         Try
  338.             If Not IsNothing(dbPar) Then
  339.                 dbPar = Nothing
  340.             End If
  341.         Catch ex As Exception
  342.             dbPar = Nothing
  343.         End Try
  344.  
  345.         Try
  346.             If Not IsNothing(dbCmd) Then
  347.                 dbCmd.Dispose()
  348.             End If
  349.         Catch ex As Exception
  350.             dbCmd = Nothing
  351.         End Try
  352.  
  353.         Try
  354.             If Not IsNothing(dbCon) Then
  355.                 If dbCon.State = ConnectionState.Open Then
  356.                     dbCon.Close()
  357.                 End If
  358.                 dbCon.Dispose()
  359.             End If
  360.         Catch ex As Exception
  361.             dbCon = Nothing
  362.         End Try
  363.  
  364.         Try
  365.             If Not IsNothing(dTable) Then
  366.                 dTable.Dispose()
  367.                 dTable = Nothing
  368.             End If
  369.         Catch ex As Exception
  370.             dTable = Nothing
  371.         End Try
  372.  
  373.         Try
  374.             If Not IsNothing(dView) Then
  375.                 dView.Dispose()
  376.                 dView = Nothing
  377.             End If
  378.         Catch ex As Exception
  379.             dView = Nothing
  380.         End Try
  381.     End Sub
  382.  
  383.     Public Shared Function Null2Space( _
  384.         ByVal strValue As Object, _
  385.         Optional ByVal blnDash As Boolean = False _
  386.     ) As String
  387.  
  388.         Try
  389.             If IsNothing(strValue) Then
  390.                 If blnDash Then
  391.                     Null2Space = "&nbsp;-&nbsp;"
  392.                 Else
  393.                     Null2Space = "&nbsp;"
  394.                 End If
  395.             ElseIf strValue Is System.DBNull.Value Then
  396.                 If blnDash Then
  397.                     Null2Space = "&nbsp;-&nbsp;"
  398.                 Else
  399.                     Null2Space = "&nbsp;"
  400.                 End If
  401.             ElseIf strValue.ToString.Trim.Length() < 1 Then
  402.                 If blnDash Then
  403.                     Null2Space = "&nbsp;-&nbsp;"
  404.                 Else
  405.                     Null2Space = "&nbsp;"
  406.                 End If
  407.             Else
  408.                 Null2Space = strValue.ToString.Trim()
  409.             End If
  410.  
  411.         Catch ex As Exception
  412.             Null2Space = strValue.ToString
  413.  
  414.         End Try
  415.     End Function
  416.  
  417. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement