OdbcConnect Function

Action

Establishes a connection to a database. Using this function, you can establish multiple connections to either a single database or to several instances at the same time.

Include file

ODBC.bdh

Syntax

OdbcConnect( in hConnection : number,
             in sDataSource : string,
             in sUserName   : string optional,
             in sPassword   : string optional ): boolean;

Return value

  • true if successful

  • false otherwise

Parameter Description
hConnection Handle to a database connection.
sDataSource Name of the data source to connect to.
sUserName User’s authorization name (optional). When the user identifier is specified in the sDataSource parameter, this parameter may be omitted.
sPassword Password for the specified authorization name (optional). When the password is specified in the sDataSource parameter, this parameter may be omitted.

Example

var
  hEnv, hDbc : number;
  cCursor : cursor;

dcltrans
  transaction TMain
  begin
    OdbcAlloc(SQL_HANDLE_ENV, hEnv);
    OdbcAlloc(SQL_HANDLE_DBC, hDbc, hEnv);
    OdbcConnect(hDbc, "DSN=database;UID=user;PWD=pass;");
    OdbcOpen(cCursor, hDbc);
    OdbcPrepare(cCursor, sqlInsert);
    OdbcBind(cCursor, ":1", SQL_C_CHAR, 32);
    OdbcBind(cCursor, ":2", SQL_C_LONG);
    OdbcSetString(cCursor, ":1", "Bob", 1);
    OdbcSetInt(cCursor, ":2", 25, 1);
    OdbcSetString(cCursor, ":1", "Marcy", 2);
    OdbcSetInt(cCursor, ":2", 33, 2);
    OdbcExecute(cCursor);
    OdbcClose(cCursor, SQL_DROP);
    OdbcCommit(hDbc);
    OdbcDisconnect(hDbc);
    OdbcFree(hDbc);
    OdbcFree(hEnv);
  end TMain;

dclsql
  sqlInsert:
    INSERT INTO persons (name, age) VALUES (?, ?);