Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Enterprise
Expertise: Beginner
Sep 8, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 \Binn\Resources\1033\sqldmo.rll under SqlServer70 directory. Now add following code and declaration in your form's code:
Private WithEvents oSqlServer As SQLDMO.SQLServer
Private Function StartSqlServer(ByVal strServerName As String, strErrorMsg As String) As Boolean
Static 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
                    Case SQLDMOSvc_Stopped
                        .Start False, strServerName, "sa"
                    Case SQLDMOSvc_Starting
                    Case SQLDMOSvc_Unknown
                        strErrorMsg = "SqlServer in unknown state"
                        Exit Do
                End Select
            If .Status = SQLDMOSvc_Running Then
                StartSqlServer = True
                StartSqlServer = False
            End If
        End With
        Set oSqlServer = Nothing
        BoolFunctionExecuting = False
    End If
End 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.

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