VB.NET - Stop SQL Server Through VB.net - Asked By Ben Sebuabe on 14-Jun-12 07:32 AM

Please I'm using Both VB.net 2008 and SQL 2008. Please how can I stop SQL server 2008 through VB.net 2008? This has been a headache to me. I thank you in advance . Ben
Jitendra Faye replied to Ben Sebuabe on 14-Jun-12 09:14 AM
For this you need to add following namespaces-

Imports Microsoft.SqlServer.Management.Smo Imports Microsoft.SqlServer.Management.Smo.Wmi Imports Microsoft.SqlServer.Management.Common

'Reference the Microsoft SQL Server service.

svc = mc.Services("MSSQLSERVER")
'Stop the service if it is running and report on the status continuously until it has stopped.
If svc.ServiceState = ServiceState.Running Then
    svc.Stop()
End if


Follow this link-

http://msdn.microsoft.com/en-us/library/ms162139%28v=sql.90%29.aspx


Hope this will help you.

RAJASEKHAR RAJENDRAN replied to Ben Sebuabe on 14-Jun-12 09:15 AM
Ben,

Try the below code and let me know.

To Start the Service.. ( The Service that runs the SQL Server 2008), use the below VB Code,

'Start Service
strServiceName = "MSSQLSERVER"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name like '" & strServiceName & "'")
For Each objService in colListOfServices
 objService.StartService()
Next

To Stop the Service..( The Service that runs the SQL Server 2008), use the below VB Code..

'Stop Service
strServiceName = "MSSQLSERVER"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name like '" & strServiceName & "'")
For Each objService in colListOfServices
 objService.StopService()
Next

Thanks & Regards,
Rajasekhar.R