Microsoft KB Archive/912426

= 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# =

Article ID: 912426

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft SQL Server 2005 Express Edition

-





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:  Start Visual Studio 2005. On the File menu, point to New, and then click Project. 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. Use ConsoleApplication1 as the name in the Name box, and then click OK. Add a reference to the &quot;System.ServiceProcess&quot; namespace. To do this, follow these steps:  On the Project menu, click Add Reference. Click the .NET tab, click System.ServiceProcess, and then click OK.</li></ol> </li>  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 = &quot;MSSQL$SQLEXPRESS&quot; 'service name of SQL Server Express Dim status As String 'service status (For example, Running or Stopped) Dim mySC As ServiceController

Console.WriteLine(&quot;Service: &quot; & 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(&quot;Service not found. It is probably not installed. [exception=&quot; & ex.Message & &quot;]&quot;) Console.ReadLine End End Try Console.WriteLine(&quot;Service status : &quot; & 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(&quot;Starting the service...&quot;) mySC.Start mySC.WaitForStatus(ServiceControllerStatus.Running) Console.WriteLine(&quot;The service is now &quot; & mySC.Status.ToString)

Catch ex As Exception Console.WriteLine(&quot;Error in starting the service: &quot; & ex.Message) End Try End If

Console.WriteLine(&quot;Press a key to end the application...&quot;) 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 = &quot;MSSQL$SQLEXPRESS&quot;; //service name of SQL Server Express string status; //service status (For example, Running or Stopped)

Console.WriteLine(&quot;Service: &quot; + 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(&quot;Service not found. It is probably not installed. [exception=&quot; + ex.Message + &quot;]&quot;); Console.ReadLine;

return;

}

//display service status: For example, Running, Stopped, or Paused Console.WriteLine(&quot;Service status : &quot; + 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(&quot;Starting the service...&quot;); mySC.Start; mySC.WaitForStatus(ServiceControllerStatus.Running); Console.WriteLine(&quot;The service is now &quot; + mySC.Status.ToString); }               catch (Exception ex) {                   Console.WriteLine(&quot;Error in starting the service: &quot; + ex.Message);

}

}

Console.WriteLine(&quot;Press a key to end the application...&quot;); Console.ReadLine;

return;

}

} } </li> Press CTRL+F5 to run the program.</li></ol>

<div class="references_section">