198 lines
8.9 KiB
HTML
198 lines
8.9 KiB
HTML
|
<?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="SQLAllocEnv - Allocate environment handle" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzadphdapi.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="rzadpfnaenv" />
|
||
|
<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>SQLAllocEnv - Allocate environment handle</title>
|
||
|
</head>
|
||
|
<body id="rzadpfnaenv"><a name="rzadpfnaenv"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">SQLAllocEnv - Allocate environment handle</h1>
|
||
|
<div><div class="section"><h4 class="sectiontitle">Purpose</h4> <p><samp class="codeph">SQLAllocEnv()</samp> allocates
|
||
|
an environment handle and associated resources.</p>
|
||
|
<p>An application must
|
||
|
call this function before <samp class="codeph">SQLAllocConnect()</samp> or any other DB2<sup>®</sup> UDB
|
||
|
CLI functions. The <em>henv</em> value is passed in all later function calls
|
||
|
that require an environment handle as input.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Syntax</h4> <pre>SQLRETURN SQLAllocEnv (SQLHENV *phenv);</pre>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Function arguments</h4>
|
||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="100%" frame="hsides" border="1" rules="all"><caption>Table 1. SQLAllocEnv arguments</caption><thead align="left"><tr><th align="left" valign="bottom" width="20%" id="d0e64">Data type</th>
|
||
|
<th align="left" valign="bottom" width="20%" id="d0e66">Argument</th>
|
||
|
<th align="left" valign="bottom" width="20%" id="d0e68">Use</th>
|
||
|
<th align="left" valign="bottom" width="40%" id="d0e70">Description</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td align="left" valign="top" width="20%" headers="d0e64 ">SQLHENV *</td>
|
||
|
<td align="left" valign="top" width="20%" headers="d0e66 "><em>phenv</em></td>
|
||
|
<td align="left" valign="top" width="20%" headers="d0e68 ">Output</td>
|
||
|
<td align="left" valign="top" width="40%" headers="d0e70 ">Pointer to environment handle</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Usage</h4> <p>There can be only one active environment
|
||
|
at any one time per application. Any later call to <samp class="codeph">SQLAllocEnv()</samp> returns
|
||
|
the existing environment handle.</p>
|
||
|
<p>By default, the first successful call
|
||
|
to <samp class="codeph">SQLFreeEnv()</samp> releases the resources associated with the
|
||
|
handle. This occurs no matter how many times <samp class="codeph">SQLAllocEnv()</samp> is
|
||
|
successfully called. If the environment attribute SQL_ATTR_ENVHNDL_COUNTER
|
||
|
is set to SQL_TRUE, SQLFreeEnv() must be called once for each successful SQLAllocEnv()
|
||
|
call before the resources associated with the handle are released. </p>
|
||
|
<p>To ensure that all DB2 UDB CLI resources are kept active, the
|
||
|
program that calls <samp class="codeph">SQLAllocEnv()</samp> should not stop or leave
|
||
|
the stack. Otherwise, the application loses open cursors, statement handles,
|
||
|
and other resources it has allocated.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Return codes</h4> <ul><li>SQL_SUCCESS</li>
|
||
|
<li>SQL_ERROR</li>
|
||
|
</ul>
|
||
|
<p>If SQL_ERROR is returned and <em>phenv</em> is equal to SQL_NULL_HENV,
|
||
|
then <samp class="codeph">SQLError()</samp> cannot be called because there is no handle
|
||
|
with which to associate additional diagnostic information.</p>
|
||
|
<p>If the return
|
||
|
code is SQL_ERROR and the pointer to the environment handle is not equal to
|
||
|
SQL_NULL_HENV, then the handle is a <em>restricted handle</em>. This means the
|
||
|
handle can only be used in a call to <samp class="codeph">SQLError()</samp> to obtain
|
||
|
more error information, or to <samp class="codeph">SQLFreeEnv()</samp>.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Diagnostics</h4>
|
||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="100%" frame="hsides" border="1" rules="rows"><caption>Table 2. SQLAllocEnv SQLSTATEs</caption><thead align="left"><tr><th align="left" valign="bottom" width="25%" id="d0e162">SQLSTATE</th>
|
||
|
<th align="left" valign="bottom" width="25%" id="d0e164">Description</th>
|
||
|
<th align="left" valign="bottom" width="50%" id="d0e166">Explanation</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td align="left" valign="top" width="25%" headers="d0e162 "><strong>58</strong>004</td>
|
||
|
<td align="left" valign="top" width="25%" headers="d0e164 ">System error</td>
|
||
|
<td align="left" valign="top" width="50%" headers="d0e166 ">Unrecoverable system error</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Example</h4></div>
|
||
|
<div class="example" id="rzadpfnaenv__xmaenv"><a name="rzadpfnaenv__xmaenv"><!-- --></a><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms
|
||
|
of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
||
|
<div class="p"><pre>/*******************************************************
|
||
|
** file = basiccon.c
|
||
|
** - demonstrate basic connection to two datasources.
|
||
|
** - error handling ignored for simplicity
|
||
|
**
|
||
|
** Functions used:
|
||
|
**
|
||
|
** SQLAllocConnect SQLDisconnect
|
||
|
** SQLAllocEnv SQLFreeConnect
|
||
|
** SQLConnect SQLFreeEnv
|
||
|
**
|
||
|
**
|
||
|
********************************************************/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include "sqlcli.h"
|
||
|
|
||
|
int
|
||
|
connect(SQLHENV henv,
|
||
|
SQLHDBC * hdbc);
|
||
|
|
||
|
#define MAX_DSN_LENGTH 18
|
||
|
#define MAX_UID_LENGTH 10
|
||
|
#define MAX_PWD_LENGTH 10
|
||
|
#define MAX_CONNECTIONS 5
|
||
|
|
||
|
int
|
||
|
main()
|
||
|
{
|
||
|
SQLHENV henv;
|
||
|
SQLHDBC hdbc[MAX_CONNECTIONS];
|
||
|
|
||
|
/* allocate an environment handle */
|
||
|
SQLAllocEnv(&henv);
|
||
|
|
||
|
/* Connect to first data source */
|
||
|
connect(henv, &hdbc[0];);
|
||
|
|
||
|
/* Connect to second data source */
|
||
|
connect(henv, &hdbc[1];);
|
||
|
|
||
|
/********* Start Processing Step *************************/
|
||
|
/* allocate statement handle, execute statement, and so on */
|
||
|
/********* End Processing Step ***************************/
|
||
|
|
||
|
printf("\nDisconnecting .....\n");
|
||
|
SQLFreeConnect(hdbc[0]); /* free first connection handle */
|
||
|
SQLFreeConnect(hdbc[1]); /* free second connection handle */
|
||
|
SQLFreeEnv(henv); /* free environment handle */
|
||
|
|
||
|
return (SQL_SUCCESS);
|
||
|
}
|
||
|
|
||
|
/********************************************************************
|
||
|
** connect - Prompt for connect options and connect **
|
||
|
********************************************************************/
|
||
|
|
||
|
int
|
||
|
connect(SQLHENV henv,
|
||
|
SQLHDBC * hdbc)
|
||
|
{
|
||
|
SQLRETURN rc;
|
||
|
SQLCHAR server[MAX_DSN_LENGTH + 1], uid[MAX_UID_LENGTH + 1],
|
||
|
pwd[MAX_PWD_LENGTH
|
||
|
+ 1];
|
||
|
SQLCHAR buffer[255];
|
||
|
SQLSMALLINT outlen;
|
||
|
|
||
|
printf("Enter Server Name:\n");
|
||
|
gets((char *) server);
|
||
|
printf("Enter User Name:\n");
|
||
|
gets((char *) uid);
|
||
|
printf("Enter Password Name:\n");
|
||
|
gets((char *) pwd);
|
||
|
|
||
|
SQLAllocConnect(henv, hdbc);/* allocate a connection handle */
|
||
|
|
||
|
rc = SQLConnect(*hdbc, server, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
|
||
|
if (rc != SQL_SUCCESS) {
|
||
|
printf("Error while connecting to database\n");
|
||
|
return (SQL_ERROR);
|
||
|
} else {
|
||
|
printf("Successful Connect\n");
|
||
|
return (SQL_SUCCESS);
|
||
|
}
|
||
|
}</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">References</h4> <ul><li><a href="rzadpfnacon.htm#rzadpfnacon">SQLAllocConnect - Allocate connection handle</a></li>
|
||
|
<li><a href="rzadpfnfenv.htm#rzadpfnfenv">SQLFreeEnv - Free environment handle</a></li>
|
||
|
<li><a href="rzadpfnastmt.htm#rzadpfnastmt">SQLAllocStmt - Allocate a statement handle</a></li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzadphdapi.htm" title="This topic provides a description of each CLI function.">DB2 UDB CLI functions</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|