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 » Understanding UltraLite Development with AppForge

Creating UltraLite databases Next Page

Connecting to an UltraLite database


UltraLite applications must connect to a database before carrying out operations on the data in it. This section describes how to connect to an UltraLite database.

Using the ULConnection object

The following properties of the ULConnection object govern global application behavior.

See ULConnection class.

Connecting to a database

You can connect to a database using either a ULConnectionParms object or a connection string. Use a ULConnectionParms object to manipulate multiple connection parameters for different target device platforms. Methods that use a connection string require you specify the different target platform strings in one large string.

For more information about connection parameters, see UltraLite Connection String Parameters Reference

The following procedure illustrates connecting to an UltraLite database:

Connect to UltraLite database
  1. Create a ULDatabaseManager object:

    You should create only one DatabaseManager object per application. This object is at the root of the object hierarchy. For this reason, it is often best to declare the DatabaseManager object as global to the application or as a class-level variable.

    'MobileVB using VB6
    Public DatabaseMgr As ULDatabaseManager
    Set DatabaseMgr = New ULDatabaseManager
    'Crossfire using vb.net
    Public DatabaseMgr As New UltraLiteAFLib.ULDatabaseManager
  2. Declare a ULConnection object:

    Most applications use a single connection to an UltraLite database, and keep the connection open all the time. For this reason, it is often best to declare the ULConnection object as global to the application.

    'MobileVB using VB6
    Public Connection As New ULConnection
    'Crossfire using vb.net
    Public Connection As UltraLiteAFLib.ULDatabaseManager
  3. Create a ULConnectionParms object:

    Double-click the ULConnectionParms object on the MobileVB tool palette. A ULConnectionParms object appears on your form.

  4. Set the required properties of the ULConnectionParms object:

    In the ULConnectionParms properties window, specify properties such as the location of the database, and a user name and password for your database.

    Using the following properties, you must specify a database file for OpenConnection.

    For information about additional properties, see Properties.

    Keyword Description
    DatabaseOnCE The path and file name of the UltraLite database on Windows CE.
    DatabaseOnDesktop The path and file name of the UltraLite database on the desktop computer.
  5. Open a connection to the database:

    OpenConnection returns an open connection as a ULConnection object. This method takes a single ULConnectionParms object as its argument.

    The following code attempts to connect to an existing database. If the database does not exist, the OpenConnection method returns an error.

    'MobileVB using VB6
    On Error Resume Next
    Set Connection = DatabaseMgr.OpenConnection(ULConnectionParms1.ToString())
    
    'Crossfire using vb.net
    Try
      Connection = _
        DatabaseMgr.OpenConnection(ULConnectionParms1.ToString())
    Catch
      If Err.Number = _
        UltraLiteAFLib.ULSQLCode.ulSQLE_ULTRALITE_DATABASE_NOT_FOUND _
      Then
        ...
    End Try
    
    // Crossfire using C#
    using UltraLiteAFLib;
    ...
      public  UltraLiteAFLib.ULConnection Connection;
      public  UltraLiteAFLib.ULDatabaseManager DatabaseMgr;
      private UltraLiteAFLib._ULConnectionParms_ingotClass parms; 
              // dropped onto design form
      parms.DatabaseOnCE = AppForge.System.AppPath + "\\mydb.udb";
      try {
            Connection = DatabaseMgr.OpenConnection( parms.ToString );
      } catch ( Exception ex ) {
        Debug.WriteLn("Connect failed: " + ex.Message );
      }