Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » UltraLite - AppForge Programming » Tutorial: A Sample Application for AppForge Crossfire » Lesson 3: Write the sample code

Lesson 3: Write the sample code Next Page

Write code to connect to your database


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 your tutcustomer database. This method is recommended. Alternatively, the database connection can be established by providing connection parameters directly as follows:

Connection = DatabaseMgr.OpenConnection("DBF=c:\tutorial\crossfire\tutCustomer.udb")

Write code to connect to the UltraLite database
  1. Double-click the form to open the Code window.

  2. Declare the required UltraLite objects.

    Immediately after the line Public NonInheritable Class CrossfireForm1 Inherits System.Windows.Forms.Form enter the following code:

    Public DatabaseMgr As New UltraLiteAFLib.ULDatabaseManager
    Public Connection As UltraLiteAFLib.ULConnection
    Public CustomerTable As UltraLiteAFLib.ULTable
  3. Specify the connection parameters.

  4. In the Form Load event CrossfireForm1_Load, add code to connect to the database.

    The standard way of connecting is to try open a connection to the database specified by the connection string. If the database does not exist, generate an error message.

    Try
       Connection = _
          DatabaseMgr.OpenConnection(ULConnectionParms1.ToString())
    Catch
       MsgBox(Err.Number)
       MsgBox(Err.Description)
    End Try
  5. Add the following code to the click event of the End button (btnDone):

    Connection.Close
    End
  6. Run the application.

Troubleshooting

You now have the basic code in place to connect to your database.

If you see a data type conversion error on the attempt to connect, make sure you have used the ToString method on the ULConnectionParms1 object.