我是这样写的:
 private void btnTest_Click(object sender, EventArgs e)
        {            ServiceController sc = new ServiceController("MSSQLSERVER");
            if (sc.Status.Equals(ServiceControllerStatus.Stopped))
            {
                sc.Start();
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            ServiceController sc = new ServiceController("MSSQLSERVER");
            if (sc.Status.Equals(ServiceControllerStatus.Running))
            {
                sc.Stop();
            }
        }
我的机器上有SQL2000和2005,这里默认的直接去找2000,我把new ServiceController("MSSQLSERVER");
改成了new ServiceController("(local)\SQLEXPRESS")不过还是不对,提示是“在计算机“.”上没有找到服务(local)\SQLEXPRESS”
怎么才能使他去测试2005呢?谢谢大家帮帮忙。

解决方案 »

  1.   

    ServiceController sc = new ServiceController("SQLEXPRESS"); 
      

  2.   

    ServiceController sc = new ServiceController("SQLEXPRESS"); 
    还是不行,也是提示“在计算机“.”上没有找到服务SQLEXPRESS”
      

  3.   

    试一试ServiceController sc = new ServiceController(".\SQLEXPRESS"); 
      

  4.   

    都试过了,提示一样的错误,我觉得它就是直接去2000连接的,根本不去找2005,要不然怎么会写“在计算机“.”上没有找到服务”,“.”不是连接的2000嘛,“.\SQLEXPRESS”才是2005呢。
      

  5.   

      ServiceController sc = new ServiceController("SQL Server (SQLEXPRESS)"); 
                if (sc.Status.Equals(ServiceControllerStatus.Running)) 
                { 
                    sc.Stop(); 
                } 
            } 测试通过
      

  6.   

    一个最简单的方法在服务里面找到sqlserverexpress的服务然后
    右键查看显示属性里面有个显示名称直接copy过来就ok了
      

  7.   

    king19840811  谢谢了,^_^
    也谢谢大家了
      

  8.   

    看我这个帖子。第一点就是你要的:
    http://topic.csdn.net/u/20090114/09/1c6425ef-b2af-4fae-927f-94f680a2e8ee.html