Let me know if this is what you need.
Imports System.IO
Imports vb = Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports Microsoft
Imports SQLDMO
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Cursor.Current = Cursors.WaitCursor
Dim curSQLServer As SQLServer
Try
Dim i, x As Integer
'For i = 0 To Me.ListBox1.Items.Count - 1
'Dim curComputer As String = Me.ListBox1.Items.Item(i)
Dim curComputer As String = "eric-cathell"
Me.sbpServerName.Text = curComputer
'------------------------------------------------------------------------------------------------
'Initialize a new instance of SQLserver for computer.
'------------------------------------------------------------------------------------------------
curSQLServer = initSQLServer(curSQLServer, curComputer)
'------------------------------------------------------------------------------------------------
'Determine if SQL server is running on this computer.
'------------------------------------------------------------------------------------------------
If Not curSQLServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running Then
curSQLServer = StartSQLServer(curSQLServer)
End If
wait(5)
Dim sproc As New SQLProcedures(curComputer)
Dim s As String = sproc.getServerName(s)
'If not (UCase(s) = UCase(curComputer)) Then
'Me.sbpServerName.Text += "-" & s
sproc.fixServername(curComputer, s)
Me.sbpServerName.Text = curComputer
curSQLServer = StopServer(curSQLServer)
wait(5)
curSQLServer = StartSQLServer(curSQLServer)
Me.sbpCurrentAction.Text = "done"
'End If
'Next
Cursor.Current = Cursors.Default
Catch ex As Exception
MessageBox.Show("userInterface.vb.Button1_Click", ex.Message,
ex.Source, ex.StackTrace)
Finally
End Try
End Sub
'========================================================================================
'DESCRIPTION: This procedure
'CONSTRUCTED BY Eric E Cathell,
'DATE: 02/17/2005,
'PROCEDURE TYPE/RETURN TYPE:
'ACTIVE PROJECT: ScaleRenamer
'ACTIVE MODULE: userInterface.vb
'ACTIVE CLASS: executeChanges
'ACTIVE PROCEDURE: executeChanges
'
'ARGUMENTS:' <no arguments>
'========================================================================================
Private Function StartSQLServer(ByVal srv As SQLServer) As SQLServer
Try
Select Case srv.Status
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Paused
srv.Continue()
Do Until srv.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running
Loop
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped
srv.Start(False)
Do Until srv.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running
Loop
End Select
wait(5)
srv.Connect()
Select Case srv.JobServer.Status
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Paused
Me.sbpCurrentAction.Text = "Start SQL Agent"
srv.JobServer.Start()
Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running
Loop
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running
MessageBox.Show("SQlAgent is already Running")
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped
Me.sbpCurrentAction.Text = "Start SQL Agent"
srv.JobServer.Start()
Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running
Loop
End Select
Return srv
Catch ex As Exception
MessageBox.Show(ex.Message, "Exception Thrown")
Finally
End Try
End Function
Private Function StopServer(ByVal srv As SQLServer) As SQLServer
Try
srv.Connect()
Select Case srv.JobServer.Status
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Paused
Me.sbpCurrentAction.Text = "Stop SQL Agent"
srv.JobServer.Stop()
Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped
Loop
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running
Me.sbpCurrentAction.Text = "Stop SQL Agent"
srv.JobServer.Stop()
Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped
Loop
Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped
MessageBox.Show("SQlAgent is already Stopped")
End Select
wait(5)
Debug.WriteLine(srv.Status.ToString)
'srv.DisConnect()
srv.Shutdown()
Catch ex As System.Runtime.InteropServices.COMException
'MessageBox.Show(ex.Message, ex.InnerException.ToString)
srv.DisConnect()
Return srv
Catch ex As Exception
MessageBox.Show(ex.Message, ex.InnerException.ToString)
Return srv
Finally
End Try
End Function
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.DoubleClick
Me.ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
Private Function initSQLServer(ByVal srv As SQLServer, ByVal name As
String) As SQLServer
Try
If Not srv Is Nothing Then
srv.DisConnect()
srv = Nothing
srv = New SQLDMO.SQLServer
Else
srv = New SQLDMO.SQLServer
End If
With srv
.LoginTimeout = 10
.Name = name
.Login =
.Password =
End With
Return srv
Catch ex As Exception
MessageBox.Show(ex.Message, "Exception Thrown")
Finally
End Try
End Function
Private Sub wait(ByVal time As Integer)
Dim t As String
t = time & "0000"
time = CInt(t)
Dim i As Integer
For i = 0 To time
Me.sbpTimer.Text = i
Next
End Sub
End Class