Obtain results

zhaozj2021-02-17  54

Obtain results

In this chapter, let's learn how to get the execution of the SQL statement.

We call a set of records returned by the query as a result set (or referred to in VB as a Recordset). Retrieval results are usually divided into the following steps:

Confirm that a result set is available. Bind the column of the result set to the appropriate variable. Take a line

After completing the operation of the result set, we must call SQLCloseCursor to destroy it.

Confirm that a result set is available

Sometimes we have known whether the result set can be used after executing the SQL statement. If the executed statement is not the type of the result set, we know that the result set is not available. But sometimes we don't know what type of SQL statement used, for example, let users enter the SQL statement itself. At this time, we must confirm if there is a result set is established, which can be implemented through the SQLNUMRESULTCOLS function. If the result set exists, the function returns the number of columns (the number of fields) in the result set. The syntax is as follows:

SQLNUMRESULTCOLS Proto StatementHandle: DWORD, PNUMCOLS: DWORD

The StatementHandle statement handle PNumcols points to a double word pointer, which contains the number of columns of the result set.

If the value points to PNumCols point to 0, then indicate that the result set does not exist.

Bind column

As with the parameters of the binding a variable to the SQL statement, we connect (bind) a variable to a column in the result set. The function you want here is SQLBINDCOL, the syntax is as follows:

SQLBINDCOL Proto StatementHandle: DWORD, ColumnNumber: DWORD, Targettype: DWORD, TARGETVALUEPTR: DWORD, BUFFERLENGTH: DWORD, PSTRLENORINDPTR: DWORD

StatementHandle Statement Handle ColumnNumber Results The number of columns to be bound in the result. Column number starts from 1. Column 0 is a bookmark column. TargetType The Type of TargetValuePtr's constant is constant. TargetValuePtr points to pointers that will be bundled to columns or buffers. When the SQLFETCH is called to obtain the row in the result set, the variable or buffer points to this parameter or the buffer will be filled in the value of the bound column. BufferLength is the length of the buffer pointing by TargetValuePtr. PSTRLENORINDPTR See the previous sep (sqlbindparameter

example:

.DATA? BUFFER DB 21 DUP (?) DATALENGTH DD?; After calling SQLFETCH, the length of the string of the buffer is filled in

.code ..... Invoke Sqlbindcol, HSTMT, 1, SQL_C_CHAR, ADDR BUFFER, 21, AddR Datalength

A line

Very simple. First adjust the SQLFETCH to retrieve a column to the bound variable. When the SQLFETCH call is complete, the cursor is updated (Updated). It can be considered a record pointer. It indicates which line will return after the SQLFetch call. For example, if the result set has 4 columns, when the result set is established, the cursor points to the first line. When the SQLFETCH is called, the cursor is added 1. So if you call SQLFETCH 4 times, there is no more row to be submitted. The cursor displays the pointing to the file (EOF). SQLFETCH syntax is as follows:

SQLFETCH PROTO StatementHandle: DWORD

This function returns SQL_NO_DATA when there is no line to be submitted.

example:

.DATA? BUFFER DB 21 DUP (?) DATALENGTH DD?

.code ..... INVOKE SQLBINDCOL, HSTMT, 1, SQL_C_CHAR, ADDR BUFFER, 21, AddR Datalength Invoke Sqlfetch, HSTMT Translator's nonsense: Remember the latitude of the earth? We use Sqlbindcol to locate longitude (column), with the cursor Define the latitude (line), use SQLFETCH to get coordinates, and result set is the earth. The big flights are not forgotten with the same one! (The captain, we can't find the water ... By the way, this is the earth? # @ & * ^ :-)

转载请注明原文地址:https://www.9cbs.com/read-30099.html

New Post(0)