Contents Index Lesson 2: Create an UltraLite schema file Lesson 4: Insert, update, and delete data

UltraLite.NET User's Guide
  Tutorial: Visual Studio Application

Lesson 3: Connect to the database


In this lesson you add code to your .NET application that connects to a database using the schema you have created.

UltraLite database files have an extension of .udb. If an application attempts to connect to a database and the specified database file does not exist, UltraLite 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 create a directory with a different name, use that directory instead of c:\tutorial\uldotnet\CSApp or c:\tutorial\uldotnet\VBApp throughout the tutorial.

To connect to an UltraLite database

  1. Right click on your form and select View Code. For a Visual C# application, add the following statement at the beginning of the file:

    using iAnywhere.UltraLite;

    This references the iAnywhere.UltraLite.NET libraries. Visual Basic projects do not need this explicit declaration.

    Add the following global variables to the form declaration of your Visual C# project, after the code describing the form components.

    private DatabaseManager DbMgr = new DatabaseManager();
    private Connection Conn;
    private PreparedStatement PrepStmt;
    private int[] ids;

    For a Visual Basic project, add the following code after the declaration of the form's components:

    Dim DbMgr As New iAnywhere.UltraLite.DatabaseManager
    Dim Conn As iAnywhere.UltraLite.Connection
    Dim PrepStmt As iAnywhere.UltraLite.PreparedStatement
    Dim ids[] As Integer
  2. Double click on a blank area of your form to create a Form1_Load method. If developing a C# application, add the following code to the method:

    try
    {
       CreateParms parms = new CreateParms();
       parms.DatabaseOnCE = 
          "\\Program Files\\CSApp\\tutorial.udb";
       parms.Schema.SchemaOnCE = 
          "\\Program Files\\CSApp\\tutorial.usm";
       try
       {
          Conn = DbMgr.OpenConnection( parms );
          MessageBox.Show( 
             "Connected to an existing database." );
       }
       catch( SQLException err )
       {
          if( err.ErrorCode == 
            SQLCode.SQLE_ULTRALITE_DATABASE_NOT_FOUND )
          {
             Conn = DbMgr.CreateDatabase( parms );
             MessageBox.Show( 
                "Connected to a new database.");
          }
          else
          {
             throw err;
          }
       RefreshLB();
       }
    }
    catch( SQLException err )
    {
       MessageBox.Show("Exception: " + err.Message +
                " sqlcode=" + err.ErrorCode);
    }
    catch ( System.Exception t )
    {
       MessageBox.Show( "Exception: " + t.Message);
    }

    For a Visual Basic application, use this code:

    Try
       Dim parms As New iAnywhere.UltraLite.CreateParms
       parms.DatabaseOnCE = _
          "\\Program Files\\VBApp\\tutorial.udb"
       parms.Schema.SchemaOnCE = _
          "\\Program Files\\VBApp\\tutorial.usm"
       Try
          Conn = DbMgr.OpenConnection(parms)
          MessageBox.Show( _
            "Connected to an existing database.")
       Catch err As iAnywhere.UltraLite.SQLException
          If err.ErrorCode = _
            SQLCode.SQLE_ULTRALITE_DATABASE_NOT_FOUND _
          Then
             Conn = DbMgr.CreateDatabase(parms)
             MessageBox.Show("Connected to a new database.")
          Else
             Throw err
          End If
       End Try
    RefreshLB()
    Catch
       MsgBox("Exception: " + err.Description)
    End Try

    This code carries out the following tasks:

  3. Build the project.

    Choose Build > Build Solution. At this stage, you should get a single error reported, which is that the name RefreshLB does not exist.

    If you get other errors, check the code until they are fixed. Check for common errors, such as case differences (for example, UltraLite, DbMgr must match case exactly).

Once the project builds with a single error, you can move to the next lesson.


Contents Index Lesson 2: Create an UltraLite schema file Lesson 4: Insert, update, and delete data