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

SQL Anywhere 10.0.1 » UltraLite - AppForge Programming » Tutorial: A Sample Application for AppForge MobileVB » Lesson 3: Write the sample code

Lesson 3: Write the sample code Next Page

Write code to connect to your database


In this application, you connect to the database during the Form_Load event. You can also connect to a database using the general module.

This example uses a ULConnectionParms object to connect to a database. Alternatively, you can use a connection string in the application code.

See ULConnectionParms class.

Write code to connect to the UltraLite database
  1. Double-click the form to open the Code window.

  2. Declare the required UltraLite objects:

    Enter the following code in the declarations area of your form.

    Public DatabaseMgr As New ULDatabaseManager
    Public Connection As ULConnection
    Public CustomerTable As ULTable
  3. Specify the connection parameters:

  4. Add code to connect to the database in the Form_Load event.

    The database manager opens a connection to the database specified by the ULConnectionParms1 object:

    Private Sub Form_Load()
    ' enable error handling
    On Error Resume Next
     
      Set Connection = DatabaseMgr.OpenConnection(ULConnectionParms1.ToString())
    
      If Err.Number = ULSQLCode.ulSQLE_NOERROR Then
        MsgBox "Connected to an existing database"
      Else
        MsgBox Err.Description
        Exit Sub
      End If
    End Sub

    Once the connection code is working, the line that issues a Msgbox to indicate a connection has been made can be removed.

    If you prefer to use a connection string rather than the ULConnectionParms object, you can alter the code illustrated above to use this syntax instead:

    Set Connection = DatabaseMgr.OpenConnection _
       ("dbf=C:\tutorial\mvb\tutcustomer.udb;" & _
        "ce_file=\tutorial\mvb\tutcustomer.udb")
    

    Note the inclusion of the database file name specification for the potential target platforms (dbf= for the desktop environment and ce_file= for the Windows CE device environment.

  5. Add code to end the application and close the connection when the End button is clicked.

    Sub btnDone_Click()
        Connection.Close
        End
    End Sub
  6. Run the application.