Restart SQL server on same machine from my application

  • Thread starter Thread starter AbanoubZak
  • Start date Start date
A

AbanoubZak

Guest
Hello,

after i set some setting in my sql server i need to restart it so i need to do this inside my app

i tried the following code

Dim service As ServiceController = New ServiceController("SQL Server (SQLEXPRESS)")
service.Stop()
service.Start()


and i got the error cannot open MSSQLSERVER service on computer, I found some people says that might need admin rights.

I'm using Click Once so i can't force my application to admin privilege so I tried to do this inside the code as following

Public Function IsRunAsAdmin() As Boolean
Try
Dim id As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim principal As WindowsPrincipal = New WindowsPrincipal(id)
Return principal.IsInRole(WindowsBuiltInRole.Administrator)
Catch __unusedException1__ As Exception
Return False
End Try
End Function

Public Sub AdminRelauncher()
If Not IsRunAsAdmin() Then
Dim proc As ProcessStartInfo = New ProcessStartInfo()
Dim procExecuting As New Process
proc.UseShellExecute = True
proc.WorkingDirectory = Environment.CurrentDirectory
proc.FileName = Assembly.GetEntryAssembly().CodeBase
proc.Verb = "runas"

Try
procExecuting = Process.Start(proc)
Application.ExitThread()
Application.Exit()
Catch ex As Exception
Console.WriteLine("This program must be run as an administrator! " & vbLf & vbLf & ex.ToString())
End Try
End If
End Sub

but still have the same error after that.

Continue reading...
 
Back
Top