<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en-us" xml:lang="en-us"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="security" content="public" /> <meta name="Robots" content="index,follow" /> <meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' /> <meta name="DC.Type" content="reference" /> <meta name="DC.Title" content="Establish ODBC connections" /> <meta name="abstract" content="Use these handle types to establish an ODBC connection." /> <meta name="description" content="Use these handle types to establish an ODBC connection." /> <meta name="DC.Relation" scheme="URI" content="rzaikodbcapiinfo.htm" /> <meta name="copyright" content="(C) Copyright IBM Corporation 1999, 2006" /> <meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1999, 2006" /> <meta name="DC.Format" content="XHTML" /> <meta name="DC.Identifier" content="estodbcconn" /> <meta name="DC.Language" content="en-us" /> <!-- All rights reserved. Licensed Materials Property of IBM --> <!-- US Government Users Restricted Rights --> <!-- Use, duplication or disclosure restricted by --> <!-- GSA ADP Schedule Contract with IBM Corp. --> <link rel="stylesheet" type="text/css" href="./ibmdita.css" /> <link rel="stylesheet" type="text/css" href="./ic.css" /> <title>Establish ODBC connections</title> </head> <body id="estodbcconn"><a name="estodbcconn"><!-- --></a> <!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script> <h1 class="topictitle1">Establish ODBC connections</h1> <div><p>Use these handle types to establish an ODBC connection.</p> <div class="section"> <dl><dt class="dlterm">SQLAllocHandle with SQL_HANDLE_ENV as the handle type</dt> <dd> <ul><li>Allocates memory for an environment handle. <ul><li>Identifies storage for global information: <ul><li>Valid connection handles</li> <li>Current<sup>®</sup> active connection handles</li> <li>Variable type HENV</li> </ul> </li> </ul> </li> <li>Must be called by application prior to calling any other ODBC function.</li> <li>Variable type HENV is defined by ODBC in the SQL.H header file provided by the C programming language compiler or by the ODBC Software Development Kit (SDK). <p>The header file contains a type definition for a far pointer: </p> <pre> typedef void far * HENV</pre> </li> <li>In C programming language this statement is coded: <pre>SQLRETURN rc; HENV henv; rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);</pre> </li> <li>In Visual Basic, this statement is coded: <pre>Dim henv As long SQLAllocEnv(henv)</pre> </li> </ul> </dd> <dt class="dlterm">SQLAllocHandle with SQL_HANDLE_DBC as the handle type</dt> <dd> <ul><li>Allocates memory for an connection handle within the environment. <ul><li>Identifies storage for information about a particular connection. <ul><li>Variable type HDBC</li> <li>Application can have multiple connection handles.</li> </ul> </li> </ul> </li> <li>Application must request a connection handle prior to connecting to the data source.</li> <li>In C, this statement is coded: <pre>HDBC hdbc; rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);</pre> </li> <li>In Visual Basic, this statement is coded: <pre>Dim hdbc As long SQLAllocConnect(henv,hdbc)</pre> </li> </ul> </dd> <dt class="dlterm">SQLSetEnvAttr</dt> <dd> <ul><li>Allows an application to set attributes of an environment.</li> <li> To be considered an ODBC 3.x application, you must set the SQL_ATTR_ODBC_VERSION to SQL_OV_ODBC3 prior to allocating a connection handle.</li> <li>In C, this statement is coded: <pre>rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);</pre> </li> </ul> </dd> <dt class="dlterm">SQLConnect</dt> <dd> <ul><li>Loads driver and establishes a connection.</li> <li>Connection handle references information about the connection.</li> <li>Data source is coded into application. <p>In C, this statement is coded: </p> <pre>SQLCHAR source[ ] = "myDSN"; SQLCHAR uid[ ] = "myUID"; SQLCHAR pwd[ ] = "myPWD"; rc = SQLConnect(hdbc, source, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);</pre> <div class="note"><span class="notetitle">Note:</span> SQL_NTS indicates that the parameter string is a null-terminated string.</div> </li> </ul> </dd> <dt class="dlterm">SQLDriverConnect</dt> <dd> <ul><li>Alternative to <strong>SQLConnect</strong></li> <li>Allows application to override data source settings.</li> <li>Displays dialog boxes (optional).</li> </ul> </dd> </dl> </div> </div> <div> <div class="familylinks"> <div class="parentlink"><strong>Parent topic:</strong> <a href="rzaikodbcapiinfo.htm" title="Identify the files required to build an ODBC application.">Files required to build an ODBC application</a></div> </div> </div> </body> </html>