UltraLite applications must connect to a database before carrying out operations on the data in it. This section describes how to connect to an UltraLite database.
The following properties of the ULConnection object govern global application behavior.
See ULConnection class.
Commit behavior By default, UltraLite applications are in AutoCommit mode. Each insert, update, or delete statement is committed to the database immediately. Set ULConnection.AutoCommit to false to build transactions into your application. Turning AutoCommit off and performing commits directly can improve the performance of your application.
See Commit method.
User authentication You can change the user ID and password for the application from the default values of DBA and sql by using the GrantConnectTo and RevokeConnectFrom methods.
See Authenticating users.
Synchronization A set of objects governing synchronization are accessed from the ULConnection object.
See Synchronizing data.
Tables UltraLite tables are accessed using the ULConnection.GetTable method.
See GetTable method.
You can connect to a database using either a ULConnectionParms object or a connection string. Use a ULConnectionParms object to manipulate multiple connection parameters for different target device platforms. Methods that use a connection string require you specify the different target platform strings in one large string.
For more information about connection parameters, see UltraLite Connection String Parameters Reference
The following procedure illustrates connecting to an UltraLite database:
Create a ULDatabaseManager object:
You should create only one DatabaseManager object per application. This object is at the root of the object hierarchy. For this reason, it is often best to declare the DatabaseManager object as global to the application or as a class-level variable.
'MobileVB using VB6 Public DatabaseMgr As ULDatabaseManager Set DatabaseMgr = New ULDatabaseManager 'Crossfire using vb.net Public DatabaseMgr As New UltraLiteAFLib.ULDatabaseManager
Declare a ULConnection object:
Most applications use a single connection to an UltraLite database, and keep the connection open all the time. For this reason, it is often best to declare the ULConnection object as global to the application.
'MobileVB using VB6 Public Connection As New ULConnection 'Crossfire using vb.net Public Connection As UltraLiteAFLib.ULDatabaseManager
Create a ULConnectionParms object:
Double-click the ULConnectionParms object on the MobileVB tool palette. A ULConnectionParms object appears on your form.
Set the required properties of the ULConnectionParms object:
In the ULConnectionParms properties window, specify properties such as the location of the database, and a user name and password for your database.
Using the following properties, you must specify a database file for OpenConnection.
For information about additional properties, see Properties.
Keyword | Description |
---|---|
DatabaseOnCE | The path and file name of the UltraLite database on Windows CE. |
DatabaseOnDesktop | The path and file name of the UltraLite database on the desktop computer. |
Open a connection to the database:
OpenConnection returns an open connection as a ULConnection object. This method takes a single ULConnectionParms object as its argument.
The following code attempts to connect to an existing database. If the database does not exist, the OpenConnection method returns an error.
'MobileVB using VB6 On Error Resume Next Set Connection = DatabaseMgr.OpenConnection(ULConnectionParms1.ToString()) 'Crossfire using vb.net Try Connection = _ DatabaseMgr.OpenConnection(ULConnectionParms1.ToString()) Catch If Err.Number = _ UltraLiteAFLib.ULSQLCode.ulSQLE_ULTRALITE_DATABASE_NOT_FOUND _ Then ... End Try // Crossfire using C# using UltraLiteAFLib; ... public UltraLiteAFLib.ULConnection Connection; public UltraLiteAFLib.ULDatabaseManager DatabaseMgr; private UltraLiteAFLib._ULConnectionParms_ingotClass parms; // dropped onto design form parms.DatabaseOnCE = AppForge.System.AppPath + "\\mydb.udb"; try { Connection = DatabaseMgr.OpenConnection( parms.ToString ); } catch ( Exception ex ) { Debug.WriteLn("Connect failed: " + ex.Message ); }