UltraLite ActiveX User's Guide
Understanding UltraLite ActiveX Development
Connecting to an UltraLite database
The following procedure connects to a database using the functions OpenConnection and CreateDatabase. These methods accept connection information as a connection string.
For more information about connecting to an UltraLite database using a connection string, see CreateDatabase method and OpenConnection method.
To connect 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 global to the application.
' eMbedded Visual Basic Dim DatabaseMgr As ULDatabaseMgr Set DatabaseMgr = CreateObject("UltraLite.ULDatabaseManager")
// JScript var DatabaseMgr; DatabaseMgr = new ActiveXObject( "UltraLite.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 global to the application.
'eMbedded Visual Basic Dim Connection As ULConnection
// JScript var Connection;
Open a connection to the database.
The ULDatabaseManager.CreateDatabase and ULDatabaseManager.OpenConnection methods return an open connection as a ULConnection object. Each method takes a single string string as its argument. The string is composed of a set of keyword=value pairs.
The following code generates a connection string that specifies the required parameters.
For more information about connection parameters, see Connection Parameters.
' eMbedded Visual Basic Dim open_parms, schema_parms As String open_parms = "ce_file=\tutorial.udb" schema_parms = open_parms & ";" & "ce_schema=\tutorial.usm"
// JScript var open_parms, schema_parms; open_parms = ";file_name=\\UltraLiteDB\\ul_custapi.udb"; schema_parms = open_parms + ";schema_file=\\UltraLiteDB\\ul_custdb.usm";
The following code fragment tries to connect to an existing database. If the database does not exist, the OpenConnection method returns an error. This causes CreateDatabase to create a new database and return a connection to it.
' eMbedded Visual Basic On Error Resume Next Set Connection = DatabaseMgr.OpenConnection(schema_parms) If Err.Number <> 0 Then Set Connection = DatabaseMgr.CreateDatabase(schema_parms) End If
// JScript var bval = DatabaseMgr.ErrorResume; DatabaseMgr.ErrorResume = true; Connection = DatabaseMgr.OpenConnection( schema_parms ); if ( DatabaseMgr.LastErrorCode != 0 ) { Connection = DatabaseMgr.CreateDatabase( schema_parms ); }