Solved

Check sql server service running & start it

Posted on 2003-10-30
11
491 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 129
Why can't I create any project with my delphi EXE file? 6 26
Dev express lookupcombo 3 30
can i put an image from resource into image tag ? 6 25
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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