Solved

Check sql server service running & start it

Posted on 2003-10-30
11
511 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
Industry Leaders: 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

Industry Leaders: 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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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