Results 1 to 5 of 5

Thread: Stop SQL Express service

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Stop SQL Express service

    Hi All.

    I have some code which backs up a sql express database using the Microsoft.SqlServer.Management.Smo namespace.

    For neatness I would like to first check to see if the service is running and stop it, perform my backup, then restart it.

    I cant figure this one out. Any ideas my lovelies ?


    TIA

    Parksie

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Stop SQL Express service

    Here's an example I just knocked up that stops the print spooler service if its running, I'm sure you can adapt it to look for the SQL Express service name instead of the print spooler

    vb Code:
    1. Dim Spoolercontroller As New ServiceProcess.ServiceController
    2. Spoolercontroller.MachineName = "."
    3. Spoolercontroller.ServiceName = "Spooler"
    4.  
    5. If Spoolercontroller.Status = ServiceProcess.ServiceControllerStatus.Running Then
    6.   Spoolercontroller.Stop()
    7. End If

    You could just drag a ServiceControl component from the toolbox onto your form to do the first 3 lines of my code in the designer but just thought it would be easier to show you in code.
    The "." that is used for the MachineName property just means "this computer" so obviously if your program was running on a different machine to where the SQL service was running then you would need to change this.

    Hope that helps
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Stop SQL Express service

    MSSQL$SQLEXPRESS is the name

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Stop SQL Express service

    Isnt that only if you used the default instance name though? (I cant remember if you even get a choice in SQL Express or if its only full blown SQL Server)
    Like if you named your SQL instance "testing" then the service would be named MSSQL$Testing wouldnt it?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Stop SQL Express service

    You can enumerate all servers to find the instance name. Here is an example function to find out the local instance on your machine
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Instance As String = GetLocalSQLService()
        End Sub
    
        Private Function GetLocalSQLService() As String
            Dim dt As DataTable = Sql.SqlDataSourceEnumerator.Instance.GetDataSources()
            For Each dr As DataRow In dt.Rows
                If dr.Item("ServerName") = My.Computer.Name Then
                    Return "MSSQL$" & dr.Item("InstanceName")
                End If
            Next
            Return String.Empty
        End Function
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width