综合

img 00x0

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

发表于2004/10/16 0:12:00  1217人阅读

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

阅读全文
0 0

相关文章推荐

img
取 消
img