Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++, Enterprise
Expertise: Intermediate
May 25, 1999

Start MS SQL Server Dynamically

Use this code in your C++ executable to start MS SQL Server dynamically on a Windows NT machine. Link to w95scm.lib, which is provided with the SQL 7.0 CD.
 
#include "wn95scm.h" //included with Sql7.0 CD
BOOL  StartSqlServer(LPSTR szErrorMsg)
{
     LPSTR       szService = "MSSQLServer";
     DWORD       dwServiceState, dwErr;
     BOOL        bControlSuccessful;

     bControlSuccessful = TRUE;    // Query service state.
     dwServiceState = SQLSCMGetLocalServiceState(szService, &dwErr);
     while ((dwServiceState != 0) && (bControlSuccessful == TRUE))
     {
          if (dwServiceState == SERVICE_RUNNING)
               break;
          switch (dwServiceState)
          {
               case SERVICE_PAUSED:     // Paused, so continue.
                    bControlSuccessful =  SQLSCMLocalServiceControl(szService,
SQLSCMCmd_CONTINUE,&dwErr, 0, NULL);
                    break;
               case SERVICE_STOPPED:     // Stopped, start service.
                    bControlSuccessful =  SQLSCMLocalServiceControl(szService,
SQLSCMCmd_START, &dwErr, 0, NULL);
                    break;
               default:// Error if state is 0 otherwise state is changing.
                    if (dwServiceState == 0)
                    {    // Error. Return failure.
                         sprintf(szErrorMsg, "Error %lu on attempt to determine
service state.", dwErr);
                         return (FALSE);
                    }
          }
          Sleep(5);
          dwServiceState = SQLSCMGetLocalServiceState(szService, &dwErr);
     }
     if (bControlSuccessful)
     {
          sprintf(szErrorMsg, "Error %lu returned on attempt to change service
state.", dwErr);
     }
     return (bControlSuccessful ? TRUE : FALSE);
}
KulBhushan Sharma
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date