I am trying to create a simple web form which will give me a service restart button onscreen.
Upon clicking the button, I am creating an SMO object to talk to a SQL Server database and trying to stop and start the MSSQLSERVER
service. All goes well until the Stop()
method is called, at which point an exception is thrown stating:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
The code under the button is as follows:
// connect the to server
ManagedComputer computer =
new ManagedComputer("172.16.150.52",@"Administrator","secret");
// return if there is a problem
if (computer.Services["MSSQLSERVER"] == null)
{
PageErrorMessage = "Bad or missing service \"MSSQLSERVER\"";
return;
}
// get the SQL Server Service
Service sqlServer = computer.Services["MSSQLSERVER"];
// is the server running?
if (sqlServer.ServiceState == ServiceState.Running)
sqlServer.Stop();
// wait for it to stop completely
int timeout = 0;
while (sqlServer.ServiceState != ServiceState.Stopped || timeout <= 60)
{
Thread.Sleep(1000);
sqlServer.Refresh();
timeout++;
}
if (timeout > 60)
{
PageErrorMessage = "Stop operation has timed out after 60secs";
return;
}
// start it again!
sqlServer.Start();
The IP address, username & password are 100% correct. Does anyone know why this would throw an AccessDenied
exception?