none
Restart SQL server on same machine from my application RRS feed

  • Question

  • 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.


    • Edited by AbanoubZak Wednesday, March 11, 2020 12:11 AM
    Wednesday, March 11, 2020 12:11 AM

Answers

  • Thanks to every one who replied. The community here is providing a huge help for me.

    I finally manged to figure out the problem my self the problem was that the code simply need like 2-3 mile seconds to execute so when i tracked the code  i found that my code manged to stop the service indeed but wasn't able to start it again as it didn't take the time for the service to fully stop.

    The solution was simple as it just needed couple of seconds to after it execute the code to stop the service.

    So simply need to add a code in between.

    First

    Imports System.Threading

    Then use the code as following

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

    You see the code where Thread.sleep is make the application wait 10 seconds before it complete to execute the code. 

    This time is depends on the machine it self, 2-3 sec was enough but some other machine needed more time so to be in the safe side just add more seconds as I did.

    Thank you, and "Stay Safe".

    • Marked as answer by AbanoubZak Thursday, March 19, 2020 4:39 AM
    Thursday, March 19, 2020 4:38 AM

All replies

  • If you're not running with the root admin account on the Windows O/S, then I doubt that you can do it. It's probably best that you force a reboot of the machine that will force SQL Server to restart on the machine.

    https://www.majorgeeks.com/content/page/how_to_activate_the_hidden_super_administrator_account_in_windows.html

    Wednesday, March 11, 2020 1:59 AM
  • In some cases a computer may have policies in place that are from Windows or a security program or company policy. In many of these cases a paid certificate (such as from VeriSign) may be needed. By using a certificate you are authenticating your program. If you go this route make sure to get instructions how to implement the certificate into your installer.

    EDIT: It's possible to run as admin and not be able to install too. As DA924 suggested a restart is another option, see this post.

    Then another option is to have the user right click on the installer and select run as admin which may or may not work.

     



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, March 11, 2020 2:01 AM
    Moderator
  •  In many of these cases a paid certificate (such as from VeriSign) may be needed. By using a certificate you are authenticating your program. If you go this route make sure to get instructions how to implement the certificate into your installer.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    I searched many times in this but the prices they asking for is too high for my needs, Do I have to get A Paid one ?

    Or I can make one as in this topic: How do I create a self-signed certificate for code signing on Windows? 

    will this work for me Instead?


    Wednesday, March 11, 2020 4:59 PM
  •  In many of these cases a paid certificate (such as from VeriSign) may be needed. By using a certificate you are authenticating your program. If you go this route make sure to get instructions how to implement the certificate into your installer.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    I searched many times in this but the prices they asking for is too high for my needs, Do I have to get A Paid one ?

    Or I can make one as in this topic: How do I create a self-signed certificate for code signing on Windows? 

    will this work for me Instead?


    Self-signing certificates will not work as anyone can create them while the paid-for certificates there are requirements behind them.

    First thing to try is have the customer try installing by right clicking on the installer and run as admin, if that works have them restart their computer which stops and starts the service.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, March 12, 2020 1:27 AM
    Moderator
  • Thanks to every one who replied. The community here is providing a huge help for me.

    I finally manged to figure out the problem my self the problem was that the code simply need like 2-3 mile seconds to execute so when i tracked the code  i found that my code manged to stop the service indeed but wasn't able to start it again as it didn't take the time for the service to fully stop.

    The solution was simple as it just needed couple of seconds to after it execute the code to stop the service.

    So simply need to add a code in between.

    First

    Imports System.Threading

    Then use the code as following

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

    You see the code where Thread.sleep is make the application wait 10 seconds before it complete to execute the code. 

    This time is depends on the machine it self, 2-3 sec was enough but some other machine needed more time so to be in the safe side just add more seconds as I did.

    Thank you, and "Stay Safe".

    • Marked as answer by AbanoubZak Thursday, March 19, 2020 4:39 AM
    Thursday, March 19, 2020 4:38 AM