Click here to Skip to main content
Click here to Skip to main content

10 Steps to Creating a Windows Mobile Database Application Using .NET and UltraLite 10

, 18 May 2007
Rate this:
Please Sign up or sign in to vote.
The purpose of this article is to help get a user started in building a mobile database application using Visual Studio .NET (C#).

Introduction

The purpose of this article is to help get a user started in building a mobile database application using UltraLite and Visual Studio .NET (C#).

If you are interested in creating a .NET database project using SQL Anywhere 10, please see my alternate article: 10 Steps to Creating a Windows Mobile Database Application Using .NET and SQL Anywhere 10.

What is UltraLite?

UltraLite is an extremely small and powerful relational database targeted at mobile devices such as Windows Mobile, Symbian, Palm and Win32. Since the database has an extremely small footprint, it is perfect for embedding in mobile database applications. Although it is typically involved in data replication, it can also be used as a simple standalone database.

Requirements

The following tools are required to get started with developing the application:

Step 1 - Create the Database

There are multiple ways to create an UltraLite database, but the easiest way is to use Sybase Central which is a GUI tool for administering database and synchronization. Open Sybase Central by choosing Start | Programs | SQL Anywhere 10 | Sybase Central.

When Sybase Central opens, you will be presented with a set of common task choices. Under UltraLite 10 choose "Create a database".

Screenshot - Sybase_Central1.jpg

This will launch a database creation wizard. Start out by pressing Next to the introduction page. In the "New database target platform" choose "Windows CE including Windows Mobile" and press Next. In the "New database file name" window enter a directory and file name such as C:\articles\SimpleUL\simpleul.udb and press Finish (NOTE: There are a number of additional options, but to keep things simple we will use the defaults).

This step should have created a new UltraLite database and connected to it within Sybase Central.

Next we will want to create a new table. To do this, right-click on the Table folder and choose New | Table.

We will name the table customers and press Finish.

Enter the following columns and save the table.

Screenshot - Sybase_Central2.jpg

You will be requested to choose a unique column for the table. This is a requirement when data replication is used. As such, choose cust_id as the unique column and press Finish.

Step 2 - Add Data to the Database

Now that we have created a new database and table we will want to enter some data to be used on the mobile device. Since the UltraLite database is binary compatible across desktops and Windows CE devices, we can add the data now and deploy the database with the applications.

To add data, click on the customers table in Sybase Central and then choose the "Data" tab in the right pane. Add 5 customers as follows by right-clicking in the right-pane and choose "Add Row". Feel free to change the data if you like.

Screenshot - Sybase_Central3.jpg

We are now done creating the UltraLite database. Close Sybase Central.

Step 3 - Create the Project

The first step is to create a new project. To do this, open Visual Studio .NET 2005 and choose New | Project. In the new project types window expand Visual C# | Smart Devices and choose "Windows Mobile 5.0 Pocket PC". In the template window, choose "Device Application". Finally name the project SimpleUL, choose a location to save the project and press OK.

Screenshot - VisualStudio1.jpg

Step 4 - Add UltraLite Namespace

Now that we have created a template C# application, we will now want to add the UltraLite namespace and reference the UltraLite database engine runtime. To do this choose Project | Add Reference. Choose the iAnywhere.Data.UltraLite (CE) component and press OK. Note: If the component is not listed, it can be found in the SQL Anywhere installation directory under \ultralite\UltraLite.NET\assembly\v2.

Screenshot - VisualStudio2.jpg

Step 5 - Add UltraLite Runtime

We will also need to include the UltraLite runtime in the application. We can do this by adding it as an "Existing Item". To do so, choose Project | Add Existing Item. In the File Name directory, switch to the SQL Anywhere 10 installation directory and choose \ultralite\ultralite.NET\ce\arm\. Next change the File Types to "All Files (*.*)". Choose ulnet10.dll and press Add.

Screenshot - VisualStudio3.jpg

In the Solution Explorer click on the ulnet10.dll file and change the "Copy to Output Directory" property to "Copy always".

Step 6 - Add UltraLite Database

We are also going to include the UltraLite database within the application. It is important to note that although we created the database using Sybase Central, there is no reason why it could not have been created on the fly when the application was initially started. The main advantage to pre-creating the database is that data can be pre-populated and included with the application.

To attach the database choose File | Add Existing Item and switch to the directory in which you saved the database from Step 1. Choose simpleul.udb and press Add. Just as we did in the previous step, choose the simpleul.udb file in the Solution Explorer and change the "Copy to Output Directory" property to "Copy always". You will also need to mark the "Build Action" property as "Content".

Step 7 - Connect to the UltraLite Database

Now that we have created and attached an UltraLite database, the next step is to add the logic to connect to the database. To do this, double click on the Form and add a reference to the UltraLite namespace by adding the following line:

using iAnywhere.Data.UltraLite;

Next we will add the following code to connect to the UltraLite database.

private ULConnection ConnUL = new ULConnection();
public Form1()
{
    InitializeComponent();
    Connect();
}
private void Connect()
{
    try
    {
        String dbf = "\\Program Files\\SimpleUL\\simpleul.udb";

        if (System.IO.File.Exists(dbf))
        {
            ConnUL.ConnectionString = "dbf=" + dbf + ";cache_size=1M";
            if (ConnUL.State != ConnectionState.Open)
            {
                ConnUL.Open();
            }
            ConnUL.DatabaseID = 1000;
        }
        else
        {
            MessageBox.Show("Database is not available", "Error");
            Application.Exit();
        }
    }
    catch (System.Exception t)
    {
        MessageBox.Show(t.Message, "Connection failed");
        return;
    }
}

Step 8 - Query the UltraLite Database

Next we will want to start adding controls to query the database. To start, we will simply select from the database. For additional capabilities, please see the SQL Anywhere 10 Online Help.

The first step will be to add a control. For this we will choose a combo box. As such drag a combo box on to the form and name it comboCustomers as follows:

Screenshot - VisualStudio4.jpg

Once again switch to the code for the Form1.cs and add the following line to the Form1 function:

RefreshList();

Next add a new function as follows:

private void RefreshList()
{
    try
    {
        using (ULCommand cmd = ConnUL.CreateCommand())
        {
            cmd.CommandText = "select last_name + ', '+ first_name from customers order by last_name";
            ULDataReader reader = cmd.ExecuteReader();

            comboCustomers.Items.Clear();
            while (reader.Read())
                comboCustomers.Items.Add(reader.GetString(0));
        }
    }
    catch (Exception err)
    {
        MessageBox.Show(
        "Exception in Pages List: " + err.Message);
    }
} 

Step 9 - Compile and Deploy the Applications

For this sample, I will use the Windows Mobile emulator as the application target. To compile and deploy, make sure the target is one of the Windows Mobile 5 emulator (top left combo box). Next choose Build | Deploy Solution. You should see the application compile, start the emulator and deployed the application.

Once the deployment is completed, you should be able to see the following files listed in the device File Explorer under \program files\simpleul.

Screenshot - WindowsMobile5_1.jpg

Step 10 - Run the Applications

From the device emulator click on the SimpleUL application. When the application launches, it will connect and populate the Customer combo box as follows:

Screenshot - WindowsMobile5_2.jpg

Next Steps

Some next steps you might want to take to enhance the application might be to:

  • Populate a text box with a customer address once a customer is chosen
  • Add the ability to update or delete a customer
  • Add database replication using MobiLink

Summary

As we have seen in the above project, UltraLite is an extremely embeddable database. As you start using the database, you will also appreciate the performance and functionality included in the database engine. The small footprint also makes it a perfect companion for any mobile database application.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Liam Cavanagh

United States United States
Liam Cavanagh currently works as a Sr. Program Manager for Microsoft focusing new cloud services in the SQL Azure organization. Within this group, Liam has created a number of new services including SQL Azure Data Sync, Microsoft Codename "Data Transfer", and Eye on Earth leveraging the Windows Azure infrastucture.
 
Additionally, he works with enterprise corporations implementing enterprise cloud and mobile solutions and conducts training seminars worldwide.
 
Liam holds a Bachelor of Mathematics degree in Business and Information Systems from the University of Waterloo in Waterloo, Ontario, Canada.
 
Specialties: Windows Azure, SQL Azure, SQL Server, Cloud, Mobile, Replication and Database Synchronization Technologies
 
Liam Cavanagh is the founder of Cotega, a data notificaton and scheduling service for cloud databases. You can contact Liam at his cloud data blog.

Comments and Discussions

 
Questiondbf files Pinmembermehdimirzaei29-Nov-11 8:50 
AnswerRe: dbf files PinmemberJohn Voclare30-Nov-11 4:25 
QuestionThe type 'System.Data.Common.DbConnection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\win_dataBase\missing_dll\missing_dl PinmemberGriffon_six5-Sep-11 9:52 
Generalthank you thank you thank you thank you PinmemberSlimFadi7-Feb-11 1:30 
GeneralListbox Pinmemberpestman30-Aug-10 20:25 
GeneralTry effiproz-cf database for windows mobile Pinmemberarith silva30-Mar-10 16:14 
GeneralThere's no data in the combobox. Pinmembereversummer17-Feb-10 22:38 
QuestionGetting no database found exception when deployed into device Pinmembersrinivas.govula1-Feb-10 20:54 
GeneralDatabase location PinmemberAliensXY16-Jan-10 8:56 
GeneralRe: Database location Pinmembersrinivas.govula1-Feb-10 21:07 
GeneralRe: Database location Pinmemberalex_us9-Sep-10 16:02 
GeneralSQL anywhere 10 developer edition Pinmemberlouietin9-Jan-10 5:57 
GeneralRe: SQL anywhere 10 developer edition PinmemberLiam Cavanagh9-Jan-10 8:13 
Generalulnet10.dll is missing PinmemberSAMCALEB27-Nov-09 0:10 
GeneralBroken ref to SQL version PinmemberMixaGr6-May-09 6:43 
GeneralProblems with download scripts PinmemberHarish197818-Feb-09 15:51 
GeneralRe: Problems with download scripts PinmemberLiam Cavanagh18-Feb-09 17:23 
QuestionSynchronization problem 2 PinmemberDinuwan Senanayake19-Jan-09 22:08 
QuestionSynchronization problem in Visual Studio .NET 2008 PinmemberDinuwan Senanayake7-Jan-09 2:28 
GeneralSynchronization problem PinmemberDinuwan Senanayake7-Jan-09 2:01 
GeneralRe: Synchronization problem PinmemberLiam Cavanagh7-Jan-09 15:02 
Questioncreate view in Mobile database Pinmemberkk_upadhyay5-Dec-07 21:40 
QuestionNeed Advice pls ... Pinmemberdani99el12-Sep-07 18:09 
AnswerRe: Need Advice pls ... PinmemberLiam Cavanagh13-Sep-07 9:05 
QuestionHow much is the license? Pinmemberianormy_home22-May-07 2:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 18 May 2007
Article Copyright 2007 by Liam Cavanagh
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid