Connecting to the Database |
Top Previous Next |
The easy database command set interfaces with the OBDC API to perform all operations on a database. Communication with the database is done through the use of a driver supplied either with the Microsoft Data Access Components (MDAC) or by the database provider. MDAC includes popular database drivers for common formats such as Access and Paradox. . Before any SQL queries, updates, insertions, etc. can be performed on a database you must first establish a connection to it. The connection is created using either the dbConnect connect function or the dbConnectDSN function. A list of database drivers currently installed can be obtained by the dbEnumDrivers function. Example: DEF pDrivers as POINTER Using dbConnect dbConnect creates a connection to a database by supply the name of the driver to use, a filename if any, and any options. dbConnect returns a POINTER type that will be NULL if the connection could not be established. The return value must be saved in a variable of type POINTER to be passed to other commands and to disconnect from the database when operations are completed. Example: pdb = dbConnect("Microsoft Access Driver (*.mdb)",GETSTARTPATH + "inventory.mdb","")
It is important to note that not every database type is discreet file based. In cases where a database is stored in a directory such as text format CSV files you supply the path to the directory in the filename parameter. Example: pdb = dbConnect("Microsoft Text Driver (*.txt; *.csv)","c:\\csvdata\\","") And some drivers just use the options parameter and a keyword to connect. Consult the documentation for your database.
Using dbConnectDSN Data Source Names (DSN) is the traditional method for connecting to a database through ODBC. While it may be traditional it is not the most convenient method unless you have a number of applications that will be using a database and don't want to worry about where the database is located. A DSN is created using the "Data Sources" control panel applet. After a DSN is created, and points to a valid database, connection to it is as easy as supplying the name and options. Example. pdb = dbConnectDSN("XYZ CORP","UID=President;PWD=Private")
Disconnecting from the database Your program must use dbDisconnect on the database before exiting. Simply pass the pointer returned by dbConnect or dbConnectDSN.
|