UltraLite ActiveX User's Guide
Tutorial: Using Dynamic SQL in an UltraLite Application for PocketPC
Lesson 3: Write the eMbedded Visual Basic sample code
In this application, you connect to the database during the Form_Load event. You can also connect to a database using the general module.
This example uses a ULConnectionParms object to connect to the database. For an example using a connection string, see Lesson 3: Write the eMbedded Visual Basic sample code.
Write code to connect to an UltraLite database
Double-click the form to open the Code window.
Declare the required UltraLite objects.
Enter the following code in the General area of your form.
Dim DatabaseMgr As ULDatabaseManager Dim Connection As ULConnection Dim myPrepStmt As ULPreparedStatement Dim MyResultSet As ULResultSet
Add code to connect to the database in the Form_Load event.
In the code below, CreateObject
is used to create the initial database manager object. It then tries to open a connection to the database specified by the ULConnectionParms object. If the database does not exist, it creates a new database using the given schema.
Sub Form_Load() ' Use CreateObject to get an instance of the Database Manager object Set DatabaseMgr = CreateObject("UltraLite.ULDatabaseManager") ' Create a LoginParms object, using CreateObject Dim LoginParms As ULConnectionParms Set LoginParms = CreateObject("UltraLite.ULConnectionParms") LoginParms.DatabaseOnCE = "\Program Files\tutorial\tutorial.udb" LoginParms.SchemaOnCE = "\Program Files\tutorial\tutorial.usm" LoginParms.CacheSize = "128k" On Error Resume Next ' Use the "WithParms" calls Set Connection = _ DatabaseMgr.OpenConnectionWithParms(LoginParms) 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 Connection = _ DatabaseMgr.CreateDatabaseWithParms(LoginParms) If Err.Number = ULSQLCode.ulSQLE_NOERROR Then MsgBox "Connected to a new database" Else MsgBox Err.Description End If End If Set MyPrepStmt = _ Connection.PrepareStatement("SELECT id, name FROM names") Set MyResultSet = MyPrepStmt.ExecuteQuery MyResultSet.MoveFirst End Sub
Write the code that ends the application and closes the connection when the End button is clicked.
Sub btnDone_Click() Connection.Close End Sub
Run the application.
Choose Run
After an initial message, the form loads.
Click OK in the top right corner to terminate the application.
Use the File Viewer to check that a database file named tutorial.udb has been created on the emulator.