Solved

Check sql server service running & start it

Posted on 2003-10-30
11
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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