Solved

Check sql server service running & start it

Posted on 2003-10-30
11
488 Views
Last Modified: 2010-04-05
I want to check at startup if the sql server service is running. The system will be running on 2000 and XP.

If it is not running I want to bring up the service manger and start it. I searched the registry to see if there was a way of checking the location of sqlservr.exe, couldn't find one. Also the available command line options for sqlservr.exe seem to indicate it can't be started from the command line. Is there a way to do this?

Thanks, Tom.
0
Comment
Question by:tomcorcoran
  • 6
  • 5
11 Comments
 
LVL 5

Accepted Solution

by:
delphized earned 200 total points
ID: 9952396
uses  
WinSvc;

procedure TForm1.Button7Click(Sender: TObject);
var
  ManHnd,
  SvcHnd:THandle;
  SvcSts:TServiceStatus;
  Err:Cardinal;
  S:PChar;
begin
  ManHnd:=OpenSCManager('','ServicesActive',GENERIC_EXECUTE);
  if ManHnd=0 then
  begin
    Err:=GetLastError;
    showmessage('Error '+inttostr(Err));
    exit;
  end;
  SvcHnd:=OpenService(ManHnd,'Alerter',SERVICE_ALL_ACCESS);
  //change alerter with the service name
  if SvcHnd=0 then
  begin
    Err:=GetLastError;
    showmessage('Error '+inttostr(Err));
    CloseServiceHandle(ManHnd);
    exit;
  end;
  ControlService(SvcHnd,SERVICE_CONTROL_INTERROGATE,SvcSts);
  if SvcSts.dwCurrentState<>SERVICE_RUNNING then
  begin
    SvcSts.dwCurrentState:=SERVICE_RUNNING;
    if StartService(SvcHnd,0,S) then
    begin
      Showmessage('Service started');
    end
    else
    begin      
      Showmessage('Service not started');
    end;
  end;
  CloseServiceHandle(SvcHnd);
  CloseServiceHandle(ManHnd);
end;
0
 

Author Comment

by:tomcorcoran
ID: 9953737
Thanks for that!  I tried it and it initially detected SvcSts.dwCurrentState=1 and set the state to SERVICE_RUNNING. I waited a while, there was no icon in the tray to indicate the service was running, so I ran again and this time the dwCurrentState reads 4 (SERVICE_RUNNING). But it's not runing because as soon as some database access is tried it says sql sever is not active. The parameters for OpenSCManager are fine as i am running sql server locally. Maybe it needs some tweaks for XP home?

Thanks, Tom.
0
 
LVL 5

Expert Comment

by:delphized
ID: 9955194
You don't see the tray Icon because the tray Icon is not managed from the service of SQL Server, It's another program and  it manages the services status (SQL Server, Sql Server Agent, DTC, Search) itself.
Now I'll have a look at what services you need to start and I'll tell you.
0
 
LVL 5

Expert Comment

by:delphized
ID: 9956422
I had a look and I saw that you have to launch the service named 'SQLSERVERAGENT', that is the agent that when started will launch SQL Server.
Try and tell me if it doesn't work
0
 
LVL 5

Expert Comment

by:delphized
ID: 9956574
Thankyou and have a good day with your SQL Server
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tomcorcoran
ID: 9956585
Great!  It works though an icon never shows in the system tray? However, the propcesses list shows sqlagent.exe and sqlservr.exe.

It works locally and I'll test it to see if I can pass the server name to OpenSCManager.

cheers, tom.
0
 
LVL 5

Expert Comment

by:delphized
ID: 9956677
The Icon in the system tray ( as I told you) is another program that is called SQLMANGR.EXE , and it's role is only to act as a user interface to the services and their configuration. If you want it  you can launch it directly or schedule at system startup. But SQL Server works correctly also without the manager icon on the tray .
0
 

Author Comment

by:tomcorcoran
ID: 9956689
Good clarification thanks. I'll test the remote server things on Friday. tom.
0
 

Author Comment

by:tomcorcoran
ID: 9956962
perlexing....the exact code that was compiling is not not...I recopied it from above just to be sure. the line "if StartService(SvcHnd,0,S) then" is causing an incompatible types "String" and "Cardinal" for the 0 parameter. I don't understand how this could suddenly start happening...

cheers, tom.
0
 
LVL 5

Expert Comment

by:delphized
ID: 9957859
try writing

WinSvc.StartService(...

and check that the S variable is a PChar ( if you want try to initialize it as S:='' )

0
 

Author Comment

by:tomcorcoran
ID: 9958323
Beauty mates!!! Don't know what was conflicting in the uses caluse but that was it!

thank you big-time, tom.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now