Microsoft Knowledge Base Email Alertz

KBAlertz.com: How to detect the status of the SQL Server Express service or start the SQL Server Express service by using Visual Basic or Visual C#

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 912426 - Last Review: May 11, 2007 - Revision: 1.1

How to detect the status of the SQL Server Express service or start the SQL Server Express service by using Visual Basic or Visual C#

SUMMARY

Microsoft SQL Server 2005 Express Edition is a service-based product. If you build Microsoft Visual Studio 2005 applications on SQL Server 2005 Express Edition, you can detect the status of the SQL Server Express service when you start the application. You can use the ServiceController class to do the following:
  • Detect the status of the SQL Server Express service.
  • Start the SQL Server Express service if it is not started correctly.
Note The default installation of SQL Server 2005 Express Edition uses an instance name of SQLEXPRESS. This instance name maps to the service name of MSSQL$SQLEXPRESS.

MORE INFORMATION

To use the ServiceController class in a Visual Studio console application to detect and to start the SQL Server Express service, follow these steps:
  1. Start Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic or Visual C# under Project types, and then click Console Application under Visual Studio installed templates.

    Note By default, the Module1.vb file is created in the Visual Basic project. By default, the Program.cs file is created in the Visual C# project.
  4. Use ConsoleApplication1 as the name in the Name box, and then click OK.
  5. Add a reference to the "System.ServiceProcess" namespace. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the .NET tab, click System.ServiceProcess, and then click OK.
  6. Replace the existing code with the following code.

    Note Replace the code in the Module1.vb file in the Visual Basic project. Replace the code in the Program.cs file in the Visual C# project.

    Visual Basic
    Imports System
    Imports System.ServiceProcess
    
    Module Module1
    
        Sub Main()
    
            Dim myServiceName As String = "MSSQL$SQLEXPRESS" 'service name of SQL Server Express
            Dim status As String  'service status (For example, Running or Stopped)
            Dim mySC As ServiceController
    
            Console.WriteLine("Service: " & myServiceName)
    
            'display service status: For example, Running, Stopped, or Paused
            mySC = New ServiceController(myServiceName)
            Try
                status = mySC.Status.ToString
            Catch ex As Exception
                Console.WriteLine("Service not found. It is probably not installed. [exception=" & ex.Message & "]")
                Console.ReadLine()
                End
            End Try
            Console.WriteLine("Service status : " & status)
    
            'if service is Stopped or StopPending, you can run it with the following code.
            If mySC.Status.Equals(ServiceControllerStatus.Stopped) Or mySC.Status.Equals(ServiceControllerStatus.StopPending) Then
                Try
                    Console.WriteLine("Starting the service...")
                    mySC.Start()
                    mySC.WaitForStatus(ServiceControllerStatus.Running)
                    Console.WriteLine("The service is now " & mySC.Status.ToString)
    
                Catch ex As Exception
                    Console.WriteLine("Error in starting the service: " & ex.Message)
                End Try
            End If
    
            Console.WriteLine("Press a key to end the application...")
            Console.ReadLine()
            End
        End Sub
    End Module
    Visual C#
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ServiceProcess;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
    
                string myServiceName = "MSSQL$SQLEXPRESS"; //service name of SQL Server Express
                string status; //service status (For example, Running or Stopped)
    
                Console.WriteLine("Service: " + myServiceName);
    
                //display service status: For example, Running, Stopped, or Paused
                ServiceController mySC = new ServiceController(myServiceName);
    
                try
                {
                    status = mySC.Status.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Service not found. It is probably not installed. [exception=" + ex.Message + "]");
                    Console.ReadLine();
    
                    return;
    
                }
    
                //display service status: For example, Running, Stopped, or Paused
                Console.WriteLine("Service status : " + status);
    
                //if service is Stopped or StopPending, you can run it with the following code.
                if (mySC.Status.Equals(ServiceControllerStatus.Stopped) | mySC.Status.Equals(ServiceControllerStatus.StopPending))
                {
                    try
                    {
                        Console.WriteLine("Starting the service...");
                        mySC.Start();
                        mySC.WaitForStatus(ServiceControllerStatus.Running);
                        Console.WriteLine("The service is now " + mySC.Status.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error in starting the service: " + ex.Message);
    
                    }
    
                }
    
                Console.WriteLine("Press a key to end the application...");
                Console.ReadLine();
    
                return;
    
            }
    
        }
    }
  7. Press CTRL+F5 to run the program.

REFERENCES

For more information about the "System.ServiceProcess" namespace, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/system.serviceprocess(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.serviceprocess(vs.71).aspx)
For more information about Visual Studio .NET, visit the following MSDN Usenet newsgroups:
http://msdn.microsoft.com/newsgroups/default.aspx (http://msdn.microsoft.com/newsgroups/default.aspx)

APPLIES TO
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft SQL Server 2005 Express Edition
Keywords: 
kbprb kbhowto KB912426
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please