Creating and Connecting a SQLServer Object

SQL-DMO

SQL-DMO

Creating and Connecting a SQLServer Object

A SQL-DMO application creates a SQLServer object and uses the Connect method when a session is required on a specific instance of Microsoft® SQL Server™. Some applications may create only a single SQLServer object, using it for all interaction with a server. Others may create multiple SQLServer objects, connected to one or more servers, providing multiple server administration functions.

SQL-DMO offers application developers flexibility in locating servers as administration targets. Regardless of the method used to identify a server, the application creates a new SQLServer object for each session.

For example, an installation routine may collect a SQL Server instance name, a system administrator user identifier, and a password as part of its functioning, as shown in the illustration.

A Microsoft Visual Basic® installation routine using the example dialog box and the Connect method of a SQLServer object might look something like:

Private Sub cmd_Install_Click()
    On Error GoTo ErrorHandler
    
    Dim oSQLServer As New SQLDMO.SQLServer
    Dim bConnected As Boolean
    
    bConnected = False
    
    oSQLServer.LoginTimeout = 30
    
    If chk_Integrated.Value = 1 Then
        oSQLServer.LoginSecure = True
        oSQLServer.Connect txt_SQLServer.Text
    Else
        oSQLServer.Connect txt_SQLServer.Text, txt_Login.Text, _
        txt_Password.Text
    End If
    
'   ... do installation ...

    oSQLServer.DisConnect
    Exit Sub
    
ErrorHandler:
    MsgBox (Err.Description)
    If bConnected = True Then
        oSQLServer.DisConnect
    End If
End Sub

Another application automating backup by using organization standard backup media and procedures may query the RegisteredServers collection of the Application object, returning the list of user-registered servers in a combo box or other control allowing selection. Based on user action, the application would use the properties of the selected RegisteredServer object when using the Connect method of a SQLServer object.

Likewise, an application could use the ListAvailableSQLServers method of the Application object to locate all instances of SQL Server in an organization.