UltraLite for MobileVB User's Guide
Tutorial: Using Dynamic SQL in an UltraLite Application for PocketPC
Lesson 3: Write connection, synchronization, and table code
In this application, you connect to the database in the Form Load event.
Write code to connect to the UltraLite database
Specify the connection parameters.
Add a ULConnectionParms control to your form. The ULConnectionParms control is a database icon on the toolbox.
In the Properties window, specify the following ULConnectionParms properties:
Property | Value |
---|---|
DatabaseOnDesktop | c:\tutorial\mvb\tutorial.udb |
DatabaseOnCE | \tutorial\mvb\tutorial.udb |
SchemaOnDesktop | c:\tutorial\mvb\tutorial.usm |
SchemaOnCE | \tutorial\mvb\tutorial.usm |
Declare the UltraLite objects you need.
Double-click the form to open the Code window.
Enter the following code at the top of the form in the declarations area. This code declares the DatabaseManager and Connection objects that you will need in this sample. It also creates two objects, a PreparedStatement and ResultSet, that you will use for data manipulation:
Dim DatabaseMgr As New ULDatabaseManager Dim MyConnection As ULConnection Dim MyPrepStmt As ULPreparedStatement Dim MyResultSet As ULResultSet
These variables will be used through the application. Note that the DatabaseMgr variable is also allocated as a new object. This is the only object that can be instantiated.
Add the code to connect to the database in the Form_Load event.
The code below opens the connection to the database and if the database is new, it assigns a schema to it. Note that we have not added extensive error handling to our code fragments. You should implement error handling in your own applications.
Sub Form_Load() On Error Resume Next Set MyConnection = _ DatabaseMgr.OpenConnectionWithParms(ULConnectionParms1) If Err.Number = ULSQLCode.ulSQLE_NOERROR Then MsgBox "Connected to an existing database" ElseIf Err.Number = _ ULSQLCode.ulSQLE_ULTRALITE_DATABASE_NOT_FOUND Then Err.Clear Set MyConnection = _ DatabaseMgr.CreateDatabaseWithParms(ULConnectionParms1) MsgBox "Connected to a new database" Else MsgBox "Connect error:" & Err.Description End If End Sub
This code attempts to connect to an existing database. If the database does not exist, it creates a new database from the schema file and establishes a connection.
For more information, see ULConnection class.
Write the code that ends the application and closes the connection when the End button is clicked:
Sub btnDone_Click() MyConnection.Close End End Sub
Run the application in the development environment.
Choose Run
After an initial message box, the form loads.
The first time you run the application, the message is Connected to a new database.
Subsequent times, the message is Connected to an existing database.
Click End to terminate the application.
Once you have successfully connected, you can comment out the message box if you wish.