SERVICE_STOPPED=1;
// Stopped == SQL //==
SERVICE_START_PENDING=2;
// Starting == //==
SERVICE_STOP_PENDING=3;
// Stopping == Check //==
SERVICE_RUNNING=4;
// Running == //==
SERVICE_CONTINUE_PENDING=5;
// Restarting after being paused == ISRun //==
SERVICE_PAUSE_PENDING=6;
// Pausing == -ning //==
SERVICE_PAUSED=7;
//Paused == //==
function SQLSCMGetLocalServiceStateA(lpszSvc: PChar;dwErr:LongWord): Integer;
//==
external 'SQLSCMGetLocalServiceStateA@files:w95scm.dll cdecl';
//==
//需要w95scm.dll动态库
function CheckMSSQLServerISSetup: Boolean;
Var //判断当前计算机是否安装MSSQQLServer
SQLPath,SQLDataRoot,SourcePath:String;
begin
Result:=False;
//得到SQL安装目录
IF RegValueExists(HKLM,'Software/Microsoft/MSSQLServer/Setup','SQLPath') then
RegQueryStringValue(HKLM,'Software/Microsoft/MSSQLServer/Setup','SQLPath',SQLPath)
else
Exit;
Result:=True;
//得到SQL数据存放路径
IF RegValueExists(HKLM,'Software/Microsoft/MSSQLServer/Setup','SQLDataRoot') then
RegQueryStringValue(HKLM,'Software/Microsoft/MSSQLServer/Setup','SQLDataRoot',SQLDataRoot);
//得到SQL安装程序所在目录
IF RegValueExists(HKLM,'Software/Microsoft/MSSQLServer/Setup','SourcePath') then
RegQueryStringValue(HKLM,'Software/Microsoft/MSSQLServer/Setup','SourcePath',SourcePath);
end;
procedure CheckMSSQLServer;
var //判断SQLServer 当前运行状态
r,e: LongWORD;
hWnd: Integer;
begin
hWnd := StrToInt(ExpandConstant('{wizardhwnd}'));
r := SQLSCMGetLocalServiceStateA('MSSQLServer',e);
case r of
SERVICE_STOPPED:
MessageBox(hWnd,'Stoped,没有启动或已停止MSSQLServer服务。',TitleCaption, MB_OK or MB_ICONINFORMATION);
SERVICE_START_PENDING:
MessageBox(hWnd,'Starting,正在启动MSSQLServer服务。',TitleCaption, MB_OK or MB_ICONINFORMATION);
SERVICE_STOP_PENDING:
MessageBox(hWnd,'Stopping,正在关闭MSSQLServer服务。',TitleCaption, MB_OK or MB_ICONINFORMATION);
SERVICE_RUNNING:
MessageBox(hWnd,'Running,MSSQLServer服务已运行。',TitleCaption, MB_OK or MB_ICONINFORMATION);
SERVICE_CONTINUE_PENDING:
MessageBox(hWnd,'Restarting,正在重新启动MSSQLServer服务。',TitleCaption, MB_OK or MB_ICONINFORMATION);
SERVICE_PAUSE_PENDING:
MessageBox(hWnd,'Pausing,正在暂停MSSQLServer服务。',TitleCaption, MB_OK or MB_ICONINFORMATION);
SERVICE_PAUSED:
MessageBox(hWnd,'Paused,MSSQLServer服务已暂停。',TitleCaption, MB_OK or MB_ICONINFORMATION);
end;
end;
function StartingSQLServer(hWnd:Integer):Boolean;
var //判断SQLServer 当前运行状态
r,e: LongWORD;
OpenTime: Integer;
SQLPath:String;
ErrorCode: Integer;
begin
Result:=False;
r := SQLSCMGetLocalServiceStateA('MSSQLServer',e);
SQLPath:='';
RegQueryStringValue(HKLM,'Software/Microsoft/MSSQLServer/Setup','SQLPath',SQLPath)
OpenTime:=0;
While ((r<>SERVICE_RUNNING) or (OpenTime<=5))do
begin
case r of
SERVICE_STOPPED:
begin
IF SQLPath='' then
begin
MessageBox(hWnd,'请启动MSSQLServer数据库服务。',TitleCaption,MB_OK or MB_ICONINFORMATION);
Sleep(20000);
End
else
begin
// InstShellExec(SQLPath+'/Binn/scm.exe','-Action 1 -Silent 1',SQLPath,SW_SHOWMINNOACTIVE,ErrorCode);
// ShowText(Hwnd,'正在启动MS SQL Server数据服务.');
// Sleep(5000);
InstExec(SQLPath+'/Binn/scm.exe','-Action 1 -Silent 1',SQLPath,true,true,SW_SHOWMINNOACTIVE,ErrorCode)
// CloseText;
end;
OpenTime:=OpenTime + 1;
end;
SERVICE_START_PENDING:
begin
// ShowText(Hwnd,'正在启动MS SQL Server数据服务.');
Sleep(5000);
OpenTime:=OpenTime + 1;
// CloseText;
end;
SERVICE_STOP_PENDING:
begin
// ShowText(Hwnd,'正在重新启动MS SQL Server数据服务.');
Sleep(5000);
IF SQLPath='' then
begin
MessageBox(hWnd,'请启动MSSQLServer数据库服务。',TitleCaption,MB_OK or MB_ICONINFORMATION);
Sleep(20000);
End
else
begin
// ShowText(Hwnd,'正在启动MS SQL Server数据服务.');
InstExec(SQLPath+'/Binn/scm.exe','-Action 1 -Silent 1',SQLPath,true,true,SW_SHOWMINNOACTIVE,ErrorCode)
// CloseText;
end;
OpenTime:=OpenTime + 1;
// CloseText;
end;
SERVICE_RUNNING:
OpenTime:=10;
SERVICE_CONTINUE_PENDING:
begin
// ShowText(Hwnd,'正在重新启动MS SQL Server数据服务.');
Sleep(5000);
OpenTime:=OpenTime + 1;
// CloseText;
end;
SERVICE_PAUSE_PENDING:
begin
// ShowText(Hwnd,'正在重新启动MS SQL Server数据服务.');
Sleep(5000);
IF SQLPath='' then
begin
MessageBox(hWnd,'请启动MSSQLServer数据库服务。',TitleCaption,MB_OK or MB_ICONINFORMATION);
Sleep(20000);
End
else
begin
/// ShowText(Hwnd,'正在启动MS SQL Server数据服务.');
InstExec(SQLPath+'/Binn/scm.exe','-Action 1 -Silent 1',SQLPath,true,true,SW_SHOWMINNOACTIVE,ErrorCode)
/// CloseText;
end;
OpenTime:=OpenTime + 1;
/// CloseText;
end;
SERVICE_PAUSED:
begin
IF SQLPath='' then
begin
MessageBox(hWnd,'请启动MSSQLServer数据库服务。',TitleCaption,MB_OK or MB_ICONINFORMATION);
Sleep(20000);
End
else
begin
// ShowText(Hwnd,'正在启动MS SQL Server数据服务.');
InstExec(SQLPath+'/Binn/scm.exe','-Action 1 -Silent 1',SQLPath,true,true,SW_SHOWMINNOACTIVE,ErrorCode)
// CloseText;
end;
OpenTime:=OpenTime + 1;
end;
end;
Sleep(5);
r := SQLSCMGetLocalServiceStateA('MSSQLServer',e);
end;
IF OpenTime=10 then
Result:=True else
result:=False;
end;