vcg03.htm

来自「Visual C++与数据库的连接经典实例」· HTM 代码 · 共 3,468 行 · 第 1/5 页

HTM
3,468
字号
<HTML>

<HEAD>

<TITLE>vcg03.htm </TITLE>

<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm">

<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/htindex.htm">

<LINK REL="Next" HREF="vcgp2.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp2.htm">

<LINK REL="Previous" HREF="vcg02.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg02.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">

<A NAME="I0"></A><P ALIGN=CENTER>

<A HREF="vcg02.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg02.htm" TARGET="_self"><IMG SRC="blanprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blanprev.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm" TARGET="_self"><IMG SRC="blantoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blantoc.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="TOC"></A>

<A HREF="vcgp2.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp2.htm" TARGET="_self"><IMG SRC="blannext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blannext.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A>


<HR ALIGN=CENTER>

<P>

<UL>

<UL>

<UL>

<LI>

<A HREF="#E68E25" >The SQL...() Functions</A>

<UL>

<LI>

<A HREF="#E69E46" >SQLAllocConnect()</A>

<LI>

<A HREF="#E69E47" >SQLAllocEnv()</A>

<LI>

<A HREF="#E69E48" >SQLAllocStmt()</A>

<LI>

<A HREF="#E69E49" >SQLBindCol()</A>

<LI>

<A HREF="#E69E50" >SQLBindParameter()</A>

<LI>

<A HREF="#E69E51" >SQLBrowseConnect()</A>

<LI>

<A HREF="#E69E52" >SQLCancel()</A>

<LI>

<A HREF="#E69E53" >SQLColAttributes()</A>

<LI>

<A HREF="#E69E54" >SQLColumnPrivileges()</A>

<LI>

<A HREF="#E69E55" >SQLColumns()</A>

<LI>

<A HREF="#E69E56" >SQLConnect()</A>

<LI>

<A HREF="#E69E57" >SQLDataSources()</A>

<LI>

<A HREF="#E69E58" >SQLDescribeCol()</A>

<LI>

<A HREF="#E69E59" >SQLDescribeParam()</A>

<LI>

<A HREF="#E69E60" >SQLDisconnect()</A>

<LI>

<A HREF="#E69E61" >SQLDriverConnect()</A>

<LI>

<A HREF="#E69E62" >SQLDrivers()</A>

<LI>

<A HREF="#E69E63" >SQLError()</A>

<LI>

<A HREF="#E69E64" >SQLExecDirect()</A>

<LI>

<A HREF="#E69E65" >SQLExecute()</A>

<LI>

<A HREF="#E69E66" >SQLExtendedFetch()</A>

<LI>

<A HREF="#E69E67" >SQLFetch()</A>

<LI>

<A HREF="#E69E68" >SQLForeignKeys()</A>

<LI>

<A HREF="#E69E69" >SQLFreeConnect()</A>

<LI>

<A HREF="#E69E70" >SQLFreeEnv()</A>

<LI>

<A HREF="#E69E71" >SQLFreeStmt()</A>

<LI>

<A HREF="#E69E72" >SQLGetConnectOption()</A>

<LI>

<A HREF="#E69E73" >SQLGetCursorName()</A>

<LI>

<A HREF="#E69E74" >SQLGetData()</A>

<LI>

<A HREF="#E69E75" >SQLGetFunctions()</A>

<LI>

<A HREF="#E69E76" >SQLGetInfo()</A>

<LI>

<A HREF="#E69E77" >SQLGetStmtOption()</A>

<LI>

<A HREF="#E69E78" >SQLGetTypeInfo()</A>

<LI>

<A HREF="#E69E79" >SQLMoreResults()</A>

<LI>

<A HREF="#E69E80" >SQLNativeSql()</A>

<LI>

<A HREF="#E69E81" >SQLNumParams()</A>

<LI>

<A HREF="#E69E82" >SQLNumResultCols()</A>

<LI>

<A HREF="#E69E83" >SQLParamData()</A>

<LI>

<A HREF="#E69E84" >SQLParamOptions()</A>

<LI>

<A HREF="#E69E85" >SQLPrepare()</A>

<LI>

<A HREF="#E69E86" >SQLPrimaryKeys()</A>

<LI>

<A HREF="#E69E87" >SQLProcedureColumns()</A>

<LI>

<A HREF="#E69E88" >SQLProcedures()</A>

<LI>

<A HREF="#E69E89" >SQLPutData()</A>

<LI>

<A HREF="#E69E90" >SQLRowCount()</A>

<LI>

<A HREF="#E69E91" >SQLSetConnectOption()</A>

<LI>

<A HREF="#E69E92" >SQLSetCursorName()</A>

<LI>

<A HREF="#E69E93" >SQLSetPos()</A>

<LI>

<A HREF="#E69E94" >SQLSetScrollOptions()</A>

<LI>

<A HREF="#E69E95" >SQLSetStmtOption()</A>

<LI>

<A HREF="#E69E96" >SQLSpecialColumns()</A>

<LI>

<A HREF="#E69E97" >SQLStatistics()</A>

<LI>

<A HREF="#E69E98" >SQLTablePrivileges()</A>

<LI>

<A HREF="#E69E99" >SQLTables()</A>

<LI>

<A HREF="#E69E100" >SQLTransact()</A></UL>

<LI>

<A HREF="#E68E26" >Using the SQL...() Functions</A>

<UL>

<LI>

<A HREF="#E69E101" >Using a Datasource</A>

<LI>

<A HREF="#E69E102" >Handling Errors in SQL...() Statements</A>

<LI>

<A HREF="#E69E103" >Quoting Names</A>

<LI>

<A HREF="#E69E104" >Getting the Datasource from the User</A></UL>

<LI>

<A HREF="#E68E27" >Summary</A></UL></UL></UL>

<HR ALIGN=CENTER>

<A NAME="E66E9"></A>

<H1 ALIGN=CENTER>

<CENTER>

<FONT SIZE=6 COLOR="#FF0000"><B>- 3 -</B>

<BR><B>Using Visual C++ Data Access Functions</B></FONT></CENTER></H1>

<BR>

<P>In Chapter 2, &quot;Understanding MFC's ODBC Database Classes,&quot; you learned about the MFC data access objects. These C++ classes were built on the data access functions that are part of the SQL library that interfaces with ODBC. These functions offer a powerful interface to ODBC.

<BR>

<P>The first part of this chapter is a reference to the SQL...() functions that make up the ODBC SDK 2.x interface. Those functions that were present in the ODBC SDK 1.0 version (such as SQLSetParam()) and that have been deleted in more recent versions of ODBC aren't covered in this chapter. The second part of this chapter presents a set of functions that you can use to access datasets. It generally is more difficult to use the SQL...() functions than to use the MFC database classes described in Chapter 2. However, these functions do offer more flexibility to the programmer who is writing an application that must access many different types of datasources, with differing tables and schema (layout and definitions).

<BR>

<P>Not withstanding the existing functionality found in the MFC ODBC classes, there is no reason why you can't use both the MFC classes and the SQL...() functions in the same code. The MFC ODBC classes have all the necessary handles to allow usage of the SQL...() functions; in fact, the MFC ODBC classes use the SQL...() functions to perform most of their database manipulation tasks.

<BR>

<BR>

<A NAME="E68E25"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>The <I>SQL...()</I> Functions</B></FONT></CENTER></H3>

<BR>

<P>This reference to the SQL...() functions will show virtually everything you need to know in order to use most of these functions. However, some functions are complex and return a substantial amount of information. If you need more information, you can refer to the various help files available (including the MSDN CD set, the ODBC SDK, and Visual C++'s help facility).

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>The SQL...() functions haven't changed substantially since their introduction. Minor changes to accommodate 32-bit programming (the original SQL...() functions were 16-bit) account for virtually all the changes found.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>When you use the SQL...() functions, remember that there is a fixed order of usage. The sample code in the second part of this chapter provides more information about how to use the functions.

<BR>

<P>Each function is presented with a prototype, function arguments (if any), an explanation of the return value (if any), a description of what the function should do, and an explanation of possible failures that might occur when you use the function. Some functions will fail for a number of reasons. When you encounter a failure, use SQLError() to determine the cause of the failure.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>Using the SQL logging facility is often very useful in determining why an SQL function failed during development. Of course, you shouldn't expect your application's users to have SQL logging turned on.

<BR>

<BR>Don't forget that SQL logging will significantly affect ODBC performance because detailed information is written to the logging file for each and every SQL operation. Don't turn on SQL logging indiscriminately; use it when you need it and then turn it off.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>In all cases where the return value is shown as type RETCODE, you should create a variable defined as this type to hold the return code. You can then use either an if() or a switch() statement to check the return code for errors.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>There are two versions of the ODBC SDK 2.1: 2.10a and 2.10b. You should use 2.10b, which was released in August of 1995, if you're using Visual C++ 2.x. The version of ODBC that is included with Visual C++ 4.0 is 2.5. Version 2.5 is intended for use on both Windows 95 and on Windows NT versions 3.5 and 3.51.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>The current version of the ODBC SDK is 2.5, which is included with both Visual C++ 4 and Visual C++ 1.5x. Microsoft hasn't announced whether (or when) further updates to the ODBC SDK will occur. It can be assumed that new versions of Visual C++ may well include new versions of ODBC.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E69E46"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B><I>SQLAllocConnect()</I></B></FONT></CENTER></H4>

<BR>

<P><B>Prototype:</B>

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">RETCODE SQLAllocConnect(HENV <I>henv</I>, HDBC FAR * <I>phdbc</I>)</FONT></PRE>

<P><B>Parameters:</B>

<BR>



<BR><CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

HENV <I>henv</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The environment handle from the call to SQLAllocEnv().

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

HDBC FAR * <I>phdbc</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A pointer to the storage for the connection handle.</FONT>

</TABLE></CENTER><P><B>Return Value:</B>

<BR>

<P>This function will return one of the following values:

<BR>



<BR><CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SQL_SUCCESS

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The function was successful.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SQL_SUCCESS_WITH_INFO

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The function was successful, and more information is available.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SQL_ERROR

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The function failed. Call SQLError() to get more information about the specific failure.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SLQ_INVALID_HANDLE

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The function failed. The handle that was passed wasn't a valid handle. Possibly, the function that created the handle had failed and didn't return a valid handle.</FONT>

</TABLE></CENTER><P>If this function fails, your SQL function should end or the error should be corrected; the function should then be re-executed.

<BR>

<P><B>Usage:</B>

<BR>

<P>The SQLAllocConnect() function is used to allocate the connection between the application and the datasource. It's called after a call to SQLAllocEnv(), and SQLAllocStmt() is called after a call to SQLAllocEnv().

<BR>

<P>You must call the SQLAllocConnect() function after getting an HENV handle from SQLAllocEnv(). Without a valid HENV handle, this function won't succeed. Always check the return code from this function for errors.

<BR>

<P><B>Notes:</B>

<BR>

<P>The function's results are placed in the handle pointed to by the <I>phdbc</I> parameter. If the SQLAllocConnect() function fails, the <I>phdbc</I> handle will be set to SQL_NULL_HDBC.

<BR>

<BR>

<A NAME="E69E47"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B><I>SQLAllocEnv()</I></B></FONT></CENTER></H4>

<BR>

<P><B>Prototype:</B>

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">RETCODE SQLAllocEnv(HENV FAR * <I>phenv</I>)</FONT></PRE>

<P><B>Parameter:</B>

<BR>



<BR><CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

HENV FAR * <I>phenv</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A pointer to an environment handle.</FONT>

</TABLE></CENTER><P><B>\Return Value:</B>

<BR>

<P>This function will return one of the following values:

<BR>



<BR><CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SQL_SUCCESS

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The function was successful.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SQL_ERROR

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The function failed. Call SQLError() to get more information about the specific failure.</FONT>

</TABLE></CENTER><P>If this function fails, your SQL function should end or the error should be corrected; the function should then be re-executed. Often, this function is the first SQL...() function that is called. If it fails, there may be no recovery.

<BR>

<P><B>Usage:</B>

<BR>

<P>Call the SQLAllocEnv() function to initialize the SQL environment. You should make a matching SQLFreeEnv() call when your calling function has finished accessing the datasource. Always check the return code from these functions for errors.

<BR>

<P><B>Notes:</B>

<BR>

<P>This function places the HENV in the supplied handle. If SQLAllocEnv() fails, the resultant <I>phenv</I> parameter is set to SQL_NULL_HENV.

<BR>

<BR>

<A NAME="E69E48"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B><I>SQLAllocStmt()</I></B></FONT></CENTER></H4>

<BR>

<P><B>Prototype:</B>

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">RETCODE SQLAllocStmt(HDBC <I>hdbc</I>, HSTMT FAR * <I>hstmt</I>)</FONT></PRE>

<P><B>Parameters:</B>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?