Can We Manage SQL Server 2000 With WMI and Visual Basic.NET? - How do you “start” or “stop” the MS SQL Server 2000 service?
(Page 2 of 5 )
Everybody knows that we can start/stop the SQL Server 2000 service in several ways. The common methods in doing so include the following:
- Using the Service Manager
- Using the “services” snap-in of “Computer Management”
- Using the “NET START/STOP” at DOS prompt
- Using the SQL-DMO based application
- Using the WMI
The last method, “Using the WMI,” has several meanings. WMI can be used not only with SQL Server 2000, but also with several other services. But now, we will concentrate on WMI with “specific” to SQL Server namespace, using only Visual Basic.NET.
The following code would make the SQL Server 2000 server start:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim PrivateLateBoundObject As ManagementObject
PrivateLateBoundObject = New ManagementObject(Nothing, New ManagementPath("\\server\root\MicrosoftSQLServer:
MSSQL_SQLServer.Name=""(local)"""), Nothing)
PrivateLateBoundObject.InvokeMethod("Start", Nothing, Nothing)
PrivateLateBoundObject.Dispose()
MsgBox("Started Succesfully")
End Sub
And here, the script is quite different from any of the scripts available in my previous article. In the above script, I started working with “InvokeMethod”, which is used to execute a WMI method dynamically! So the following is the most important statement within the above code:
PrivateLateBoundObject.InvokeMethod("Start", Nothing, Nothing)
The line above starts the MS SQL Server 2000 service immediately. If it is already started and you still try to start it, I don’t think you will run into a problem. Anyway, try checking for yourself.
And I hope everyone can easily figure out how to stop the MS SQL Server 2000 service. The following code does the same:
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Dim PrivateLateBoundObject As ManagementObject
PrivateLateBoundObject = New ManagementObject(Nothing, New ManagementPath("\\server\root\MicrosoftSQLServer:
MSSQL_SQLServer.Name=""(local)"""), Nothing)
PrivateLateBoundObject.InvokeMethod("Stop", Nothing, Nothing)
PrivateLateBoundObject.Dispose()
MsgBox("Stopped Succesfully")
End Sub
Next: Can we retrieve all logins available in SQL Server 2000? >>
More MS SQL Server Articles
More By Jagadish Chaterjee