Solved

Check sql server service running & start it

Posted on 2003-10-30
11
482 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thankyou and have a good day with your SQL Server
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:tomcorcoran
Comment Utility
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
Comment Utility
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
Comment Utility
Good clarification thanks. I'll test the remote server things on Friday. tom.
0
 

Author Comment

by:tomcorcoran
Comment Utility
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
Comment Utility
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
Comment Utility
Beauty mates!!! Don't know what was conflicting in the uses caluse but that was it!

thank you big-time, tom.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

13 Experts available now in Live!

Get 1:1 Help Now