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 - .NET Programming » Tutorial: Build an UltraLite.NET Application

Lesson 2: Create an UltraLite database Next Page

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.

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. Open the source code for the form.

    Double-click the form to open the source file (Form1.cs).

  2. 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 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 Form1 method declaration.

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

    These variables are used as follows:

  4. 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 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 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 choose 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 class or namespace 'CSApp.Form1' because RefreshListBox is not 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.