?? odbc-e2.htm
字號:
<html>
<head>
<title>ODBC Programming Tutorial: Connecting to the Data Source</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#000000" text="#FFFFFF" link="#FFFFCC" vlink="#FFCCCC" alink="#CCFFCC">
<h1 align="center"><font face="Tahoma" color="#FFFFCC">Connecting to Data Source</font></h1>
<p align="left"><font face="Tahoma" size="-1">In this tutorial, we learn the mechanics
of using ODBC APIs.</font></p>
<p align="left"><font face="Tahoma" size="-1">Your program doesn't talk directly
to the ODBC drivers. It talks to the ODBC manager. The ODBC manager defines
a set of APIs your program can call to direct it to do the job for you. In your
program, you need to include odbc32.inc and odbc32.lib. Also you need to include
windows.inc.</font></p>
<p align="left"><font face="Tahoma" size="-1">The steps in connecting to the data
source are as follows:</font></p>
<ol>
<li><font face="Tahoma" size="-1"><b><font color="#FFFFCC">Allocate an environment
handle</font></b>. You need to do this only once per ODBC session. Once you
obtain the handle, you can modify the environment properties to suit your
particular needs. You can think of this step as creating the workspace for
your DB job.</font></li>
<li><font color="#FFFFCC" face="Tahoma" size="-1"><b>Indicate what version of
ODBC your program wants to use</b></font><font face="Tahoma" size="-1">. You
can choose between ODBC version 2.x and 3.x. They are different in many respects
thus this step is necessary so the ODBC manager can decide which syntax it
should use to communicate with your program and interpret the commands from
your program.</font></li>
<li><font face="Tahoma" size="-1"><b><font color="#FFFFCC">Allocate a connection
handle</font></b>. This step can be viewed as creating an empty connection.
You haven't specify what driver you want to use and which database you need
to connect. Such information will be filled in later.</font></li>
<li><font face="Tahoma" size="-1"><b><font color="#FFFFCC">Establish a connection</font></b>.
You call an ODBC function to establish the connection.</font></li>
</ol>
<p><font face="Tahoma" size="-1">When you are done with the connection, you must
close and destroy it in the following steps:</font></p>
<ol>
<li><font color="#FFFFCC" face="Tahoma" size="-1"><b>Disconnect from the data
source</b></font><font face="Tahoma" size="-1">.</font></li>
<li><font face="Tahoma" size="-1"><b><font color="#FFFFCC">Destroy the connection
handle</font></b>.</font></li>
<li><font color="#FFFFCC" face="Tahoma" size="-1"><b>Destroy the environment
handle</b></font><font face="Tahoma" size="-1"> (if you don't want to use
this environment for more connections)</font></li>
</ol>
<h4><font face="Tahoma" color="#CCFFCC">Allocating a Handle</font></h4>
<p><font face="Tahoma" size="-1">In ODBC versions prior to 3.x, you need to call
separate functions for allocating environment, connection, and statement handles
(<font color="#FFFFCC"><b>SQLAllocEnv</b></font>, <font color="#FFFFCC"> <b>SQLAllocConnect</b></font>,
<font color="#FFFFCC"><b>SQLAllocStmt</b></font>). Now under ODBC 3.x, those
functions are superseded by <font color="#FFFFCC"><b>SQLAllocHandle</b></font>
which has the following syntax:</font></p>
<blockquote>
<pre><font face="Tahoma"><b><font color="#CCFFCC">SQLRETURN SQLAllocHandle( SQLSMALLINT HandleType,
SQLHANDLE InputHandle,
SQLHANDLE * OutputHandlePtr
); </font></b></font></pre>
</blockquote>
<p><font face="Tahoma" size="-1">The above line may look daunting. I will simplify
it for you.</font> </p>
<blockquote>
<pre><font face="Tahoma" color="#CCFFCC"><b>SQLAllocHandle proto HandleType:DWORD, <br> InputHandle:DWORD, <br> OutputHandlePtr:DWORD</b></font></pre>
</blockquote>
<p><font color="#FFFFCC" face="Tahoma" size="-1"><b>SQLRETURN</b></font><font face="Tahoma" size="-1">
is defined as type <font color="#FFFFCC"><b>SQLSMALLINT</b></font>. And<font color="#FFFFCC"><b>
SQLSMALLINT</b></font> is defined as a short integer, ie. a word (16 bits).
So the function returns the value in <font color="#FFFFCC"><b>ax</b></font>,
not<font color="#FFFFCC"><b> eax</b></font>. This is important. However, parameter
passing to a function under Win32 is done via the 32-bit stack. Thus even if
the parameter is defined as a word-size one, you must extend it to 32-bit. That's
why <font color="#CCFFCC"><b>HandleType</b></font> is a dword instead of a word.
You can check with the import lib, <font color="#CCFFCC"> <b>odbc32.lib</b></font>.
The entry for <font color="#FFFFCC"><b>SQLAllocHandle</b></font> is <font color="#CCFFCC"><b>_SQLAllocHandle@12</b></font>.
Which means the combined size of parameters for this function is 12 bytes (3
dwords). However, this doesn't mean that the C function prototype is incorrect.
<font color="#FFFFCC"> <b>SQLAllocHandle</b></font> will only use the low word
of <font color="#CCFFCC"><b>HandleType</b></font> and ignore the high word.
Thus the C function prototype is <font color="#CCCCFF"><i><b>functionally</b></i></font>
correct while our asm function prototype reflects practicality.</font></p>
<p><font face="Tahoma" size="-1">With SQL type discussion out of the way, we can
turn our attention to the function parameters and the return value.</font></p>
<ul>
<li><font face="Tahoma" size="-1"><b><font color="#CCFFCC">HandleType</font></b>
is a constant that defines what type of handle you want to allocate. The possible
values are:</font></li>
</ul>
<table border="1" cellspacing="1" cellpadding="3" align="center">
<tr>
<td bgcolor="#0000CC"><font face="Tahoma" size="-1"><b>SQL_HANDLE_ENV </b></font></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">Environment handle</font></td>
</tr>
<tr>
<td bgcolor="#0000CC"><font face="Tahoma" size="-1"><b>SQL_HANDLE_DBC</b></font></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">Connection handle</font></td>
</tr>
<tr>
<td bgcolor="#0000CC"><font face="Tahoma" size="-1"><b>SQL_HANDLE_STMT</b></font></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">Statement handle</font></td>
</tr>
<tr>
<td bgcolor="#0000CC"><font face="Tahoma" size="-1"><b>SQL_HANDLE_DESC</b></font></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">Descriptor handle</font></td>
</tr>
</table>
<blockquote>
<p><font face="Tahoma" size="-1">A descriptor is a collection of metadata that
describes the parameters of an SQL statement or the columns of a result set,
as seen by the application or driver</font></p>
</blockquote>
<ul>
<li><font color="#CCFFCC" face="Tahoma" size="-1"><b>InputHandle</b></font><font face="Tahoma" size="-1">
is the handle of the parent "context". That is, if you want to allocate
a connection handle, you need to pass an environment handle because the connection
will be made in the context of that environment. If you want to allocate an
environment handle, this parameter must be <font color="#CCFFCC"><b>SQL_HANDLE_NULL</b></font>
(beware of the value of <font color="#CCFFCC"><b>SQL_HANDLE_NULL</b></font>
in windows.inc version 1.18 and below. It is defined improperly as 0L. You
need to delete the "L" else your program will not assemble. The
fault is mine alone since I'm the one who updated the SQL/ODBC part of windows.inc.)
because there is no parent context for an environment. As for the statement
and descriptor handles, you must pass the connection handle as this parameter
as both statement and descriptor occur in the context of a connection</font></li>
<li><font color="#CCFFCC"><b><font face="Tahoma" size="-1">OutputHandlePtr</font></b></font><font face="Tahoma" size="-1">
points to a dword variable that will receive the allocated handle if the call
is successful.</font></li>
</ul>
<p><font face="Tahoma" size="-1">The possible return values of <font color="#FFFFCC"><b>SQLAllocHandle</b></font>
can be:</font></p>
<table border="1" cellspacing="1" cellpadding="3" align="center">
<tr>
<td bgcolor="#003399"><b><font face="Tahoma" size="-1">SQL_SUCCESS</font></b></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">The function completed
successfully.</font></td>
</tr>
<tr>
<td bgcolor="#003399"><b><font face="Tahoma" size="-1">SQL_SUCCESS_WITH_INFO</font></b></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">The function completed
successfully but with possible non-fatal errors (warnings). </font></td>
</tr>
<tr>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -