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.
Open the source code for the form.
Double-click the form to open the source file (Form1.cs).
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
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:
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.Double-click a blank area of your form to create a Form1_Load method.
This method performs the following tasks:
Opens a connection to the database using the connection parameters set in the ulConnectionParms1 control.
Calls the RefreshListBox method, which you will define later in this tutorial.
If an error occurs, prints the error message. For SQL errors, the code also prints the error code.
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
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.