Start SQLServer Dynamically with
VB and SQL-DMO Objects

Create a new standard exe project and add reference of sqldmo.rll. This file can be found in BinnResources1033sqldmo.rll under SqlServer70 directory.Now add following code and declaration in your form’s code:

 Private WithEvents oSqlServer As SQLDMO.SQLServerPrivate Function StartSqlServer(ByVal strServerName As String, strErrorMsg As String) As BooleanStatic boolFunctionExecuting As Boolean    If Not boolFunctionExecuting Then        BoolFunctionExecuting = True        Set oSqlServer = New SQLServer        With oSqlServer            .Name = strServerName            Do While .Status <> SQLDMOSvc_Running                Select Case .Status                    Case SQLDMOSvc_Paused                        .Continue                    Case SQLDMOSvc_Stopped                        .Start False, strServerName, "sa"                    Case SQLDMOSvc_Starting                        DoEvents                    Case SQLDMOSvc_Unknown                        strErrorMsg = "SqlServer in unknown state"                        Exit Do                End Select            Loop            If .Status = SQLDMOSvc_Running Then                StartSqlServer = True            Else                StartSqlServer = False            End If        End With        Set oSqlServer = Nothing        BoolFunctionExecuting = False    End IfEnd Function

This function works quite well and if you would like to enhance it, use following constants for .Status property :
SQLDMOSvc_Continuing, SQLDMOSvc_Pausing, SQLDMOSvc_Starting, SQLDMOSvc_Stopped, SQLDMOSvc_Stopping.

You can also write code for the events fired by SQLServer object.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: