DELPHI 7 (WIN32) 与VC#启动SQL SERVER 2000服务

类别:.NET开发 点击:0 评论:0 推荐:

DELPHI WIN32使用Service API和SQL SERVER自备服务工具scm来启动SQL SERVER
unit Unit2;

interface
  procedure RunMSSQLSERVICE ;
implementation
uses
  Windows,WinSvc;
procedure RunMSSQLSERVICE ;
   var
    SrvHandle: SC_HANDLE;
    Service_Status: _SERVICE_STATUS;
    SrvStatus : Integer;
  begin
   TRY
    SrvHandle := OpenSCManager('', SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
    SrvHandle := OpenService(SrvHandle, PChar('MSSQLServer'), SERVICE_QUERY_STATUS or SERVICE_START);
    if  QueryServiceStatus(SrvHandle, Service_Status) then
       begin

         SrvStatus := Service_Status.dwCurrentState;
         if   SrvStatus = SERVICE_STOPPED   then
             begin
               Winexec('scm -action 1 -slient 1 -service mssqlserver ',sw_Normal);
             end;
       end;
   EXCEPT
   END;   
  end;

end.
-------------------------------------------------------------------------------------------

VC# 2005 EXPRESS在.NET框架下只需封装了serviceAPI的serviceController组件帮忙
#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace WindowsApplication1
{
    partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.serviceController1.ServiceName = "MSSQLServer";
                if (this.serviceController1.Status != System.ServiceProcess.ServiceControllerStatus.Stopped)
                {
                    this.serviceController1.Start();
                }
            }
            catch
            {

            }
        }
    }
}

基本上原理归为使用系统提供的 SERVICE API或WMI接口 启动,以及使用SQL SERVER自带工具scm来启动.
最简单的办法是SCM命令行方法scm -action 1 -slient 1 -service mssqlserver ',sw_Normal

本文地址:http://com.8s8s.com/it/it43528.htm