วิธีการตรวจสอบสถานะของบริการ SQL Server Express หรือเริ่มการทำงานของบริการ SQL Server Express โดยใช้ Visual Basic หรือ Visual c#

หมายเลขบทความ (Article ID): 912426 - ผลิตภัณฑ์ที่เกี่ยวข้องในบทความนี้
ขยายทั้งหมด | ยุบทั้งหมด

สรุป

Microsoft SQL Server 2005 Express Edition เป็นผลิตภัณฑ์ที่ใช้บริการ ถ้าคุณสร้างโปรแกรมประยุกต์ของ Microsoft Visual Studio 2005 ใน Edition ด่วนของ SQL Server 2005 คุณสามารถตรวจพบสถานะของบริการ SQL Server Express เมื่อคุณเริ่มการทำงานของแอพลิเคชัน คุณสามารถใช้ได้ServiceControllerคลาทำต่อไปนี้:
  • ตรวจสอบสถานะของบริการ SQL Server Express
  • เริ่มบริการ SQL Server Express ถ้าโปรแกรมไม่เริ่มทำงานอย่างถูกต้อง
หมายเหตุ:การติดตั้งเริ่มต้นของ Edition ด่วนของ SQL Server 2005 ใช้ชื่ออินสแตนซ์ของ SQLEXPRESS ชื่ออินสแตนซ์นี้แมปกับชื่อบริการของ SQLEXPRESS $ MSSQL

ข้อมูลเพิ่มเติม

เมื่อต้องการใช้การServiceControllerคลาในโปรแกรมประยุกต์คอนโซล Visual Studio เพื่อตรวจหา และเริ่มการทำงานของบริการ SQL Server Express ทำตามขั้นตอนเหล่านี้:
  1. เริ่ม Visual Studio 2005
  2. ในการแฟ้ม:เมนู ให้ชี้ไปที่ใหม่แล้ว คลิกProject.
  3. คลิกvisual Basicหรือvisual c#ภายใต้ชนิดโครงการแล้ว คลิกแอพลิเคชันของคอนโซลภายใต้แม่แบบการติดตั้ง visual Studio.

    หมายเหตุ:โดยค่าเริ่มต้น แฟ้ม Module1.vb ถูกสร้างขึ้นในโครงการ Visual Basic โดยค่าเริ่มต้น แฟ้ม Program.cs ถูกสร้างขึ้นในโครงการ Visual c#
  4. การใช้ConsoleApplication1ชื่อที่อยู่ในนั้นชื่อ:กล่อง แล้วคลิกตกลง.
  5. เพิ่มการอ้างอิงถึง namespace "System.ServiceProcess" โดยให้ทำตามขั้นตอนต่อไปนี้::
    1. ในการProjectเมนู คลิกเพิ่มการอ้างอิง.
    2. คลิกการ.NETแท็บ คลิกSystem.ServiceProcessแล้ว คลิกตกลง.
  6. เปลี่ยนรหัสที่มีอยู่กับรหัสต่อไปนี้

    หมายเหตุ:แทนโค้ดในแฟ้ม Module1.vb ในโครงการ Visual Basic แทนที่รหัสใน Program.cs แฟ้มในโครงการ Visual c#

    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 เพื่อเรียกใช้โปรแกรม

ข้อมูลอ้างอิง

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ namespace "System.ServiceProcess" แวะไปที่เว็บไซต์ของ Microsoft สำหรับนักพัฒนาเครือข่าย (MSDN) ต่อไปนี้:
.aspx http://msdn2.microsoft.com/en-us/library/system.serviceprocess (vs.71)
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ Visual Studio .NET แวะไปที่กลุ่มข่าวสาร Usenet MSDN ต่อไปนี้:
http://msdn.microsoft.com/newsgroups/default.aspx

คุณสมบัติ

หมายเลขบทความ (Article ID): 912426 - รีวิวครั้งสุดท้าย: 19 ตุลาคม 2553 - Revision: 1.0
ใช้กับ
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional
  • Microsoft SQL Server 2005 Express Edition
Keywords: 
kbprb kbhowto kbmt KB912426 KbMtth
แปลโดยคอมพิวเตอร์
ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:912426

ให้ข้อเสนอแนะ