精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>数据库技术>>判断MS SQL Server是否启动(两种方法)

主题:判断MS SQL Server是否启动(两种方法)
发信人: soaringbird(假行僧*飞翔鸟)
整理人: teleme(2001-07-07 10:31:56), 站内信件
//窗体
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 442
  Height = 246
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 216
    Top = 40
    Width = 32
    Height = 13
    Caption = 'Label1'
  end
  object Button1: TButton
    Left = 32
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 48
    Top = 104
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
  end
  object cs: TClientSocket
    Active = False
    Address = '202.99.0.172'
    ClientType = ctNonBlocking
    Port = 1433
    Left = 32
    Top = 48
  end
  object Timer1: TTimer
    Interval = 5000
    OnTimer = Timer1Timer
    Left = 80
    Top = 48
  end
end


////////////代码
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ScktComp, ExtCtrls;

const
  SERVICE_STOPPED=1; // Stopped
  SERVICE_START_PENDING=2; // Starting
  SERVICE_STOP_PENDING=3; // Stopping
  SERVICE_RUNNING=4; // Running
  SERVICE_CONTINUE_PENDING=5; // Restarting after being paused
  SERVICE_PAUSE_PENDING=6; // Pausing
  SERVICE_PAUSED=7; //Paused

type
  TForm1 = class(TForm)
    cs: TClientSocket;
    Button1: TButton;
    Timer1: TTimer;
    Label1: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  function SQLSCMGetLocalServiceStateA(lpszSvc: PChar;dwErr:PDWORD): Integer;cdecl;external 'w95scm.dll';

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  cs.Open;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  try
    cs.Active := true;
  finally
    if cs.Active then
      Label1.Caption := 'Runnig'
    else
      Label1.Caption := 'Not Runnig';
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  r,e: DWORD;
begin
  r := SQLSCMGetLocalServiceStateA('MSSQLServer',@e);
  case r of
    SERVICE_STOPPED:
      ShowMessage('Stoped');
    SERVICE_START_PENDING:
      ShowMessage('Starting');
    SERVICE_STOP_PENDING:
      ShowMessage('Stopping');
    SERVICE_RUNNING:
      ShowMessage('Running');
    SERVICE_CONTINUE_PENDING:
      ShowMessage('Restarting');
    SERVICE_PAUSE_PENDING:
      ShowMessage('Pausing');
    SERVICE_PAUSED:
      ShowMessage('Paused');
  end;
end;

end.


----
高举马列主义、毛泽东思想、邓小平理论伟大红旗,紧紧团结在以江泽民同志为核心的党中央周围,奋勇前进!前进!前进!进!进!进!

[关闭][返回]