?? odbc-e4.htm
字號:
<html>
<head>
<title>ODBC Programming Tutorial: Retrieving Results</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">Retrieving Results</font></h1>
<p><font face="Tahoma" size="-1">In this tutorial, you will learn how to retrieve
the records returned by executing an SQL statement.</font></p>
<p><font face="Tahoma" size="-1">We call a group of records returned by a query<font color="#CCFFCC"><b>
a result set</b></font> (or recordset for those who are familiar with VB). The
general steps in retrieving a result set are as follows:</font></p>
<ol>
<li><font face="Tahoma" size="-1">Determine whether a result set is available.</font></li>
<li><font face="Tahoma" size="-1">Bind the columns of the result set to appropriate
variables</font></li>
<li><font face="Tahoma" size="-1">Fetching a row</font></li>
</ol>
<p><font face="Tahoma" size="-1">When you're done with the result set, you need
to destroy it by calling <font color="#FFFFCC"><b>SQLCloseCursor</b></font>.</font></p>
<h4><font face="Tahoma" color="#FFFFCC">Determine whether a result set is available</font></h4>
<p><font face="Tahoma" size="-1">Sometimes you already know whether a result set
will be created by examination of the SQL statement. If the SQL statement is
not of a type that returns a result set, you know that no result set will be
available. However, sometimes you don't even know of what type the SQL statement
is, such is the case when you let the user enters custom SQL statements. In
that case, you must check whether a result set was created by calling <font color="#FFFFCC"><b>SQLNumResultCols</b></font>.
This function returns the number of columns (fields) in the result set (if one
exists) and has the following syntax:</font></p>
<blockquote>
<pre><b><font face="Tahoma" color="#33CCCC">SQLNumResultCols proto StatementHandle:DWORD, pNumCols:DWORD</font></b></pre>
</blockquote>
<ul>
<li><font face="Tahoma" size="-1"><b><font color="#FFCCFF">StatementHandle</font></b>
The handle to the statement</font></li>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">pNumCols</font></b></font><font face="Tahoma" size="-1">
A pointer to a dword variable that will receive the number of columns in the
result set.</font></li>
</ul>
<p><font face="Tahoma" size="-1">If the value in the variable pointed to by<font color="#FFCCFF"><b>
pNumCols</b></font> is 0, there is no result set.</font></p>
<h4><font face="Tahoma" color="#FFFFCC">Bind the columns</font></h4>
<p><font face="Tahoma" size="-1">In this regard, the concept is identical to that
of binding a variable to a parameter of an SQL statement. You associate (bind)
a variable to a specific column in the result set. The function in this case
is <font color="#FFFFCC"><b>SQLBindCol</b></font> which has the following syntax:</font></p>
<blockquote>
<pre><b><font face="Tahoma" color="#CCFFCC">SQLBindCol proto StatementHandle:DWORD,<br> ColumnNumber:DWORD,<br> TargetType:DWORD,<br> TargetValuePtr:DWORD,<br> BufferLength:DWORD,<br> pStrLenOrIndPtr:DWORD</font></b></pre>
</blockquote>
<ul>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">StatementHandle</font></b></font><font face="Tahoma" size="-1">
The handle to the statement</font></li>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">ColumnNumber</font></b></font><font face="Tahoma" size="-1">
Number of the column in the result set to bind. The column number starts from
1. Column 0 is the bookmark column.</font></li>
<li><font face="Tahoma" size="-1"><b><font color="#FFCCFF">TargetType </font></b>The
constant that indicates the type of the variable (buffer) pointed to by<font color="#FFCCFF"><b>
TargetValuePtr</b></font>.</font></li>
<li><font face="Tahoma" size="-1"><b><font color="#FFCCFF">TargetValuePtr</font></b>
A pointer to the variable or buffer that will be bound to the column. When
you call <font color="#FFFFCC"><b>SQLFetch</b></font> to retrieve a row from
the result set, the variable or buffer will be filled with the value in the
associated column.</font></li>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">BufferLength </font></b></font><font face="Tahoma" size="-1">The
size of the buffer pointed to by <font color="#FFCCFF"><b>TargetValuePtr</b></font>.</font></li>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">pStrLenOrIndPtr</font></b></font><font face="Tahoma" size="-1">
Look up the detail in <font color="#FFFFCC"><b>SQLBindParameter</b></font></font></li>
</ul>
<p><font face="Tahoma" size="-1"><b><font color="#33CCCC">Example:</font></b></font></p>
<p><font color="#33CCCC"><b><font face="Tahoma" size="-1">.data?<br>
buffer db 21 dup(?)<br>
DataLength dd ? ; will be filled with the length
of the string in buffer after SQLFetch is called.</font></b></font></p>
<p><font color="#33CCCC"><b><font face="Tahoma" size="-1">.code<br>
.....<br>
invoke SQLBindCol, hStmt, 1, SQL_C_CHAR, addr
buffer, 21, addr DataLength</font></b></font></p>
<h4><font face="Tahoma" color="#FFFFCC">Fetching a row</font></h4>
<p><font face="Tahoma" size="-1">It's quite simple. Calling <font color="#FFFFCC"><b>SQLFetch</b></font>
retrieves a row from the result set into the bound variables. After <font color="#FFFFCC"><b>SQLFetch</b></font>
is called, the cursor is updated. You can think of a cursor as the record pointer.
It indicates what row will be returned when <font color="#FFFFCC"><b>SQLFetch</b></font>
is called. For example, if the result set has 4 rows, the cursor is positioned
at the first row when the result set was created. When <font color="#FFFFCC"><b>SQLFetch</b></font>
is called, the cursor advances by 1 row. So if you call <font color="#FFFFCC"><b>SQLFetch
</b></font>4 times, there is no more row to fetch. The cursor is said to point
to the end of file (EOF). <font color="#FFFFCC"><b>SQLFetch</b></font> has the
following syntax:</font></p>
<blockquote>
<pre><b><font face="Tahoma" color="#33CCCC">SQLFetch proto StatementHandle:DWORD</font></b></pre>
</blockquote>
<p><font face="Tahoma" size="-1">This function returns<font color="#CCFFCC"><b>
SQL_NO_DATA</b></font> when no more row is available.</font></p>
<p><font face="Tahoma" size="-1"><b><font color="#33CCCC">Example:</font></b></font></p>
<p><font color="#33CCCC"><b><font face="Tahoma" size="-1">.data?<br>
buffer db 21 dup(?)<br>
DataLength dd ?</font></b></font></p>
<p><font color="#33CCCC"><b><font face="Tahoma" size="-1">.code<br>
.....<br>
invoke SQLBindCol, hStmt, 1, SQL_C_CHAR, addr
buffer, 21, addr DataLength<br>
invoke SQLFetch, hStmt</font></b></font></p>
<hr>
<p align="center"><font face="Tahoma" size="-1"><b>[<a href="http://win32asm.cjb.net">Iczelion's
Win32 Assembly Homepage</a>]</b></font></p>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -