The connection type

Description

connection is the type identifier for a built-in object type, whose values identify instances of connection objects. As the name suggests, instances of connection objects are used to connect to database engines, currently connections can only be made via ODBC or to MySQL databases.

The connection object type has the following methods:

The connection object type has the following properties:

The built-in procedure new is used to create instances of connection objects, and the built in procedure dispose is used to detroy instances of connection objects.

Example

The simple program below illustrates how to connect to a database engine using the connection object type.

program Data(input, output);
var
   conn : connection;

   procedure DisplayError(msg : string);
   begin
      writeln('ERROR:',msg);
      halt
   end;

   function GetConnectionString : string;
   var
      s, sDSN, sUSER, sPassword : string;

      function GetStringValue(sPrompt : string) : string;
      var
         sValue : string;
      begin
         write(sPrompt);
         readln(sValue);
         GetStringValue := sValue
      end;

   begin
      if supported(feature_odbc) then
         begin
            sDSN := GetStringValue('Enter Data Source Name (DSN):');
            sUser := GetStringValue('Enter user id:');
            sPassword := GetStringValue('Enter password:');
            s := 'ODBC;DSN='+sDSN+';user='+sUser+';password='+sPassword;
         end
      else if supported(feature_mysql) then
         begin
            sUser := GetStringValue('Enter user id:');
            sPassword := GetStringValue('Enter password:');
            s := 'MYSQL;user="'+sUser+'";password="'+sPassword+'";socket="/tmp/mysql.sock"';
         end
      else
         DisplayError('No database support detected');
      GetConnectionString := s;
   end;

begin
   new(conn);
   conn.open(GetConnectionString);
   //
   //Add code here to process database
   //
   conn.close;
   dispose(conn);
end.

Portability

Operating Systems: All
Standard Pascal: No