Collection Contents Previous Next PDF

UltraLite.NET User's Guide

Tutorial: Build an UltraLite.NET Application

Lesson 3: Connect to the database


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

If an UltraLite application attempts to connect to a database that does not exist, it uses the schema file to create the database.

This tutorial assumes that if you are designing a C# application, your files are in the directory c:\tutorial\uldotnet\CSApp and that if you are designing a Visual Basic application, your files are in the directory c:\tutorial\uldotnet\VBApp. If you created a directory with a different name, use that directory throughout the tutorial.

To add an UltraLite connection to your application

  1. Add the ConnectionParms control to your form.

    From the UltraLite.NET CF tab in the toolbox, choose ULConnectionParms and add it to your form. The control is named UlConnectionParms1 (Visual Basic) or ulConnectionParms1 (C#).

    If you have not rebooted since installing SQL Anywhere Studio, you may get an error message at this point. Reboot your machine to solve this problem.

  2. Specify the connection parameters.

    Ensure the following properties are set:

  3. Open the source code for the form.

    Double-click the form to open the source file.

  4. 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
  5. Add global variables to the form declaration.

    For a Visual C# application, 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 project, add the following code after the declaration of the form components (FriendsWithEvents declarations) and before the first method declaration.

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

    These variables are used as follows:

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

    This method performs the following tasks.

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

    //Visual C#
    try {
        Conn = new ULConnection( 
            ulConnectionParms1.ToString() );
        Conn.OpenWithCreate();
        Conn.DatabaseID = 1;
        RefreshListBox();
    }
    catch ( System.Exception t ) {
        MessageBox.Show( "Exception: " + t.Message);
    }

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

    'Visual Basic
    Try
        Conn = New ULConnection( _
            UlConnectionParms1.ToString())
        Conn.OpenWithCreate()
        Conn.DatabaseID = 1
        RefreshListBox()
    Catch
        MsgBox("Exception: " + err.Description)
    End Try
  7. Build the project.

    From the Build menu choose Build Solution. At this stage, you should get a single error reported, which is that the Name RefreshListBox is not declared. You will fix this error by adding the function in the next lesson.

    If you get other errors, fix them. Check for common errors, such as case inconsistencies in C#. For example, UltraLite and ULConnection must match case exactly.


Collection Contents Previous Next PDF