Lesson 3: Connecting to the database

The following procedure adds a control to your UltraLite.NET application that establishes a connection to an UltraLite database.

 Add an UltraLite connection to your application
  1. Double-click the form to open the source file (Form1.cs or Form1.vb).

  2. Add code to import the iAnywhere.Data.UltraLite namespace.

    Add the following statement as the very first line of the file.

    //Visual C#
    using iAnywhere.Data.UltraLite;
    'Visual Basic
    Imports iAnywhere.Data.UltraLite
  3. Add global variables to the form declaration.

    For Visual C#, add the following code after the code describing the form components and before the first method declaration.

    //Visual C#
    private ULConnection Conn;
    private int[] ids;

    For a Visual Basic, add the following code at the beginning of the Form1 class.

    'Visual Basic
    Dim Conn As ULConnection
    Dim ids() As Integer

    These variables are used as follows:

    • ULConnection   A Connection object is the root object for all actions executed on a connection to a database.

    • ids   The ids array is used to hold the ID column values returned after executing a query.

      Although the ListBox control itself allows you access to sequential numbers, those numbers differ from the value of the ID column once a row has been deleted. For this reason, the ID column values must be stored separately.

  4. Double-click a blank area of your form to create a Form1_Load method.

    This method performs the following tasks:

    • Open a connection to the database using the connection parameters set in the ulConnectionParms1 control.

    • Call the RefreshListBox method (defined later in this tutorial).

    • Print (display) and error message if an error occurs. For SQL Anywhere errors, the code also prints the error code. See Error Messages.

    For C#, add the following code to the Form1_Load method.



    //Visual C#
    try {
        String ConnString = "dbf=\\Program Files\\CSApp\\CSApp.udb";
        Conn = new ULConnection( ConnString );
        Conn.Open();
        Conn.DatabaseID = 1;
        RefreshListBox();
    }
    catch ( System.Exception t ) {
        MessageBox.Show( "Exception: " + t.Message);
    }

    For Visual Basic, add the following code to the Form1_Load method.



    'Visual Basic
    Try
        Dim ConnString as String = "dbf=\Program Files\VBApp\VBApp.udb"
        Conn = New ULConnection( ConnString )
        Conn.Open()
        Conn.DatabaseID = 1
        RefreshListBox()
    Catch
        MsgBox("Exception: " + err.Description)
    End Try
  5. Build the project.

    From the Build menu, click Build Solution. At this stage, you may receive a single error reported; for example in C#: error CS0103: The name 'RefreshListBox' does not exist in the current context. because RefreshListBox is not yet declared. The next lesson adds that function.

    If you get other errors, you must correct them before proceeding. Check for common errors, such as case inconsistencies in C#. For example, UltraLite and ULConnection must match case exactly. In Visual Basic it is crucial to include the Imports iAnywhere.Data.UltraLite statement as described in Lesson 3.