Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (Français) » SQL Anywhere Server - Programming » SQL Anywhere Data Access APIs » SQL Anywhere OLE DB and ADO development » ADO programming with SQL Anywhere

 

Connecting to a database with the Connection object

This section describes a simple Visual Basic routine that connects to a database.

Sample code

You can try this routine by placing a command button named Command1 on a form, and pasting the routine into its Click event. Run the program and click the button to connect and then disconnect.

Private Sub cmdTestConnection_Click( _
        ByVal eventSender As System.Object, _
        ByVal eventArgs As System.EventArgs) _
        Handles cmdTestConnection.Click
 
    ' Declare variables
    Dim myConn As New ADODB.Connection
    Dim myCommand As New ADODB.Command
    Dim cAffected As Integer

    On Error GoTo HandleError

    ' Establish the connection
    myConn.Provider = "SAOLEDB"
    myConn.ConnectionString = _
        "Data Source=SQL Anywhere 11 Demo"
    myConn.Open()
    MsgBox("Connection succeeded")
    myConn.Close()
    Exit Sub

HandleError:
    MsgBox(ErrorToString(Err.Number))
    Exit Sub
End Sub
Notes

The sample carries out the following tasks:

  • It declares the variables used in the routine.

  • It establishes a connection, using the SQL Anywhere OLE DB provider, to the sample database.

  • It uses a Command object to execute a simple statement, which displays a message in the database server messages window.

  • It closes the connection.

When the SAOLEDB provider is installed, it registers itself. This registration process includes making registry entries in the COM section of the registry, so that ADO can locate the DLL when the SAOLEDB provider is called. If you change the location of your DLL, you must re-register it.

♦  To register the OLE DB provider
  1. Open a command prompt.

  2. Change to the directory where the OLE DB provider is installed.

  3. Enter the following commands to register the provider:

    regsvr32 dboledb11.dll
    regsvr32 dboledba11.dll

For more information about connecting to a database using OLE DB, see Connecting to a database using OLE DB.