Hi,
Refer below code.
https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2005/ms162139%28v=sql.90%29
C#
protected void StopSQL(object sender, EventArgs e)
{
ManagedComputer mc = new ManagedComputer();
Service svc;
svc = mc.Services("MSSQLSERVER");
if (svc.ServiceState == ServiceState.Running)
{
svc.Stop();
Response.Write(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState));
while (string.Format("{0}", svc.ServiceState) != "Stopped")
{
Response.Write(string.Format("{0}", svc.ServiceState));
svc.Refresh();
}
Response.Write(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState));
svc.Start();
while (string.Format("{0}", svc.ServiceState) != "Running")
{
Response.Write(string.Format("{0}", svc.ServiceState));
svc.Refresh();
}
Response.Write(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState));
}
else
{
Response.Write("SQL Server service is not running.");
}
}
VB.Net
Protected Sub StopSQL(ByVal sender As Object, ByVal e As EventArgs)
Dim mc As ManagedComputer = New ManagedComputer()
Dim svc As Service
For Each svc In mc.Services
Response.Write(svc.Name)
Next
svc = mc.Services("MSSQLSERVER")
If svc.ServiceState = ServiceState.Running Then
svc.Stop()
Response.Write(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))
Do Until String.Format("{0}", svc.ServiceState) = "Stopped"
Response.Write(String.Format("{0}", svc.ServiceState))
svc.Refresh()
Loop
Response.Write(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))
svc.Start()
Do Until String.Format("{0}", svc.ServiceState) = "Running"
Response.Write(String.Format("{0}", svc.ServiceState))
svc.Refresh()
Loop
Response.Write(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))
Else
Response.Write("SQL Server service is not running.")
End If
End Sub