Friday, March 9, 2012

Quick question on VB

Hey folks,

I am trying to run a batchfile or a external process ( exe ) from within a Script Task and read the process status returned by the process. Is there a sample VB script code that does this that you guys can share ? What modules do I have to Import ?

Thanks

-chiraj

This should do the trick. To test this, create a batch file in C:\temp called test.bat. Simply put "foobar" or something invalid in the batch file to create an errorlevel. Copy the code into a script task, run the package. It will show you the errorlevel in a message box.

Imports System

Imports System.Data

Imports System.Math

Imports System.Windows.Forms

Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

Public Sub Main()

Dim stat As Integer = ExecProc("C:\\temp\\test.bat")

MessageBox.Show("The exit status was : " + stat.ToString())

Dts.TaskResult = Dts.Results.Success

End Sub

Public Function ExecProc(ByVal Path As String) As Integer

Dim objProc As System.Diagnostics.Process

Dim status As Integer

Try

objProc = New System.Diagnostics.Process()

objProc.StartInfo.FileName = Path

objProc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal

objProc.Start()

'Wait until the process passes back an exit code

objProc.WaitForExit()

status = objProc.ExitCode

Catch Ex As Exception

MessageBox.Show(Ex.Message)

Finally

'Release the objProc resources

objProc.Close()

End Try

Return status

End Function

End Class

HTH,

Kirk Haselden
Author "SQL Server Integration Services"

|||Thank you, Kirk.

No comments:

Post a Comment