文章編號: 912426 - 上次校閱: 2007年5月11日 - 版次: 1.2

如何偵測 SQL Server Express 服務的狀態,或使用 Visual Basic 或 Visual C# 啟動 SQL Server Express 服務

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
全部展開 | 全部摺疊

結論

Microsoft SQL Server 2005 Express 的版是一項服務為基礎的產品。如果您建置 SQL Server 2005 Express 版的 Microsoft Visual Studio 2005 應用程式,可以在當您啟動應用程式時偵測 SQL Server Express 服務的狀態。您可以使用 ServiceController 類別,執行下列動作:
  • 偵測到 SQL Server Express 服務的狀態。
  • 如果不正確地啟動,請啟動 SQL Server Express 服務。
附註SQL Server 2005 Express 版的預設安裝使用 SQLEXPRESS 的執行個體名稱。這個執行個體名稱會對應到 MSSQL $ SQLEXPRESS 服務名稱。

其他相關資訊

若要使用 Visual Studio 主控台應用程式中的 ServiceController 類別,以偵測並若要啟動 SQL Server Express] 服務,請依照下列步驟執行:
  1. 啟動 Visual Studio 2005。
  2. 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。
  3. 按一下 [專案類型,] 下的 [Visual BasicVisual C#,然後按一下 [Visual Studio 安裝範本] 下的 [主控台應用程式

    附註預設情況下,Module1.vb 檔案是在 Visual Basic 專案中建立。預設情況下,Program.cs 檔案是在 Visual C# 專案中建立。
  4. 使用 ConsoleApplication1 作為在 [名稱] 方塊中的名稱,然後按一下 [確定]
  5. 加入 System.ServiceProcess"命名空間的參考。要這麼做,請您執行下列步驟:
    1. 在 [專案] 功能表上按一下 新增參考
    2. 按一下 [.NET] 索引標籤,按一下 [System.ServiceProcess,然後再按一下 [確定]
  6. 下列程式碼取代現有的程式碼。

    附註取代在 Visual Basic 專案 Module1.vb 檔案中的程式碼。Visual C# 專案中的 [Program.cs 中的程式碼檔案的取代。

    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. 按下 CTRL + F5 執行程式。

?考

如需有關 System.ServiceProcess"命名空間的詳細資訊,請造訪下列 Microsoft 開發 o 人 h 員 ? 工 u 具 ? 網路 (MSDN) 網站]:
http://msdn2.microsoft.com/en-us/library/system.serviceprocess(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.serviceprocess(vs.71).aspx)
如需有關 Visual Studio.NET 的詳細資訊,請造訪下列 MSDN Usenet 新聞群組:
http://msdn.microsoft.com/newsgroups/default.aspx (http://msdn.microsoft.com/newsgroups/default.aspx)

這篇文章中的資訊適用於:
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft SQL Server 2005 Express Edition
關鍵字:?
kbmt kbprb kbhowto KB912426 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:912426? (http://support.microsoft.com/kb/912426/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。