?? odbc-e2.htm
字號:
<td bgcolor="#003399"><b><font face="Tahoma" size="-1">SQL_ERROR</font></b></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">The function failed.</font></td>
</tr>
<tr>
<td bgcolor="#003399"><b><font face="Tahoma" size="-1">SQL_INVALID_HANDLE</font></b></td>
<td bgcolor="#006666"><font face="Tahoma" size="-1">The handle passed to the
function is invalid.</font></td>
</tr>
</table>
<p><font face="Tahoma" size="-1">Whether the function succeeded or failed, you
can obtain more information about it by calling <font color="#FFFFCC"><b>SQLGetDiagRec</b></font>
or <font color="#FFFFCC"><b>SQLGetDiagField</b></font>. They serve the same
role as <font color="#FFFFCC"><b>GetLastError</b></font> in Win32 API.</font></p>
<p><font face="Tahoma" size="-1"><br>
<font face="MS Sans Serif"><b><font face="Tahoma" color="#33CCCC">Example:</font></b></font></font></p>
<p><font face="Tahoma" color="#33CCCC"><b><font size="-1">.data?<br>
hEnv dd ?</font></b></font></p>
<p><font face="Tahoma" color="#33CCCC"><b><font size="-1">.code<br>
invoke SQLAllocHandle, SQL_HANDLE_ENV, SQL_HANDLE_NULL,
addr hEnv<br>
.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO</font></b></font></p>
<h4><font face="Tahoma" color="#FFFFCC">Choosing ODBC version</font></h4>
<p><font face="Tahoma" size="-1">After allocating an environment handle, you need
to set an environment attribute, <font color="#CCFFCC"><b>SQL_ATTR_ODBC_VERSION</b></font>,
to the appropriate value. "Attributes" are just variables. Setting
the value of an environment attribute is done by calling <font color="#FFFFCC"><b>SQLSetEnvAttr</b></font>.
By now you should be able to guess that there are also<font color="#FFFFCC"><b>
SQLSetConnectAttr</b></font> and <font color="#FFFFCC"><b>SQLSetStmtAttr</b></font>.<font color="#FFFFCC"><b>
SQLSetEnvAtt</b></font>r is defined as:</font></p>
<blockquote>
<pre><font face="Tahoma"><b><font color="#CCFFCC">SQLSetEnvAttr proto EnvironmentHandle:DWORD,<br> Attribute:DWORD,<br> ValuePtr:DWORD,
StringLength:DWORD</font></b></font></pre>
</blockquote>
<ul>
<li><font color="#FFCCFF" face="Tahoma" size="-1"><b>EnvironmentHandle</b></font><font face="Tahoma" size="-1">.
As the name speaks for itself, it contains the handle to the environment which
attribute you want to set.</font></li>
<li><font color="#FFCCFF" face="Tahoma" size="-1"><b>Attribute</b></font><font face="Tahoma" size="-1">.
A constant that represents the attribute you want to set. For our purpose,
it's <font color="#CCFFCC"><b>SQL_ATTR_ODBC_VERSION</b></font>. You can look
up the full list from MSDN. </font></li>
<li><font color="#FFCCFF" face="Tahoma" size="-1"><b>ValuePtr</b></font><font face="Tahoma" size="-1">.
The meaning of this parameter depends on the attribute you want to set. If
the attribute is a 32-bit value, this parameter is treated as the value you
want to set. If the attribute is a text string or a binary buffer, it is interpreted
as the pointer to the string or the buffer. If you specify <font color="#CCFFCC"><b>SQL_ATTR_ODBC_VERSION</b></font>,
there are two possible values that you can use: <font color="#CCFFCC"><b>SQL_OV_ODBC3</b></font>
and <font color="#CCFFCC"><b>SQL_OV_ODBC2</b></font>, for ODBC version 3.x
and 2.x respectively.</font></li>
<li><font color="#FFCCFF" face="Tahoma" size="-1"><b>StringLength</b></font><font face="Tahoma" size="-1">.
The size of the value pointed to by <font color="#FFCCFF"><b>ValuePtr</b></font>.
If the value is a string or a binary buffer, this parameter must be valid.
If the attribute you want to set is a dword, this parameter is ignored. Since
<font color="#CCFFCC"><b>SQL_ATTR_ODBC_VERSION</b></font> attribute contains
a dword value, you can pass NULL as this parameter.</font></li>
</ul>
<p><font face="Tahoma" size="-1">The list of possible return values is identical
to that of <font color="#FFFFCC"><b>SQLAllocHandle</b></font>.</font></p>
<p><font face="Tahoma" size="-1"><font face="MS Sans Serif"><b><font face="Tahoma" color="#33CCCC">Example:</font></b></font></font></p>
<p><font face="Tahoma" color="#33CCCC"><b><font size="-1">.data?<br>
hEnv dd ?</font></b></font></p>
<p><font face="Tahoma" color="#33CCCC"><b><font size="-1">.code<br>
invoke SQLAllocHandle, SQL_HANDLE_ENV, SQL_HANDLE_NULL,
addr hEnv<br>
.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO<br>
invoke SQLSetEnvAttr,
hEnv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, NULL<br>
.if ax==SQL_SUCCESS ||
ax==SQL_SUCCESS_WITH_INFO</font></b></font></p>
<h4><font face="Tahoma" color="#FFFFCC">Allocating a connection handle</font></h4>
<p><font face="Tahoma" size="-1">This step is quite similar to allocating the
environment handle. You also call <font color="#FFFFCC"><b>SQLAllocHandle</b></font>
but pass to it different parameter values.</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>
hEnv dd ?<br>
hConn dd ?</font></b></font></p>
<p><font color="#33CCCC"><b><font face="Tahoma" size="-1">.code<br>
invoke SQLAllocHandle, SQL_HANDLE_ENV, SQL_HANDLE_NULL,
addr hEnv<br>
.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO<br>
invoke SQLSetEnvAttr,
hEnv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, NULL<br>
.if ax==SQL_SUCCESS ||
ax==SQL_SUCCESS_WITH_INFO<br>
invoke
SQLAllocHandle, SQL_HANDLE_DBC, hEnv, addr hConn<br>
.if
ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO</font></b></font></p>
<h4><font face="Tahoma" color="#FFFFCC">Establish a connection</font></h4>
<p><font face="Tahoma" size="-1">We are now ready to attempt the actual connection
to the data source via selected ODBC driver. There are actually three ODBC functions
we can use to achieve that goal. They offer varying degrees of "choices"
you can make.</font></p>
<table border="1" cellspacing="1" cellpadding="3" align="center">
<tr>
<td bgcolor="#003399"><font face="Tahoma" size="-1">SQLConnect</font></td>
<td bgcolor="#666600"><font face="Tahoma" size="-1">Core</font></td>
<td bgcolor="#660066"><font face="Tahoma" size="-1">This is the simplest function.
It needs only the DSN (Data source name) and optional user name and password.
It doesn't offer any GUI options such as prompting the user with a dialog
box for more information. You should use this function if you already have
a DSN for the required database.</font></td>
</tr>
<tr>
<td bgcolor="#003399"><font face="Tahoma" size="-1">SQLDriverConnect</font></td>
<td bgcolor="#666600"><font face="Tahoma" size="-1">Core</font></td>
<td bgcolor="#660066"><font face="Tahoma" size="-1">This function offers more
support than <b>SQLConnect</b>. You can connect to a data source that is
not defined in the system information, ie. without DSN. Furthermore, you
can specify whether this function will display a dialog box prompting the
user for more information. For example, if you omitted the filename of the
database, it will instruct the ODBC driver to display a dialog box prompting
the user to select the database to connect.</font></td>
</tr>
<tr>
<td bgcolor="#003399"><font face="Tahoma" size="-1">SQLBrowseConnect</font></td>
<td bgcolor="#666600"><font face="Tahoma" size="-1">Level 1</font></td>
<td bgcolor="#660066"><font face="Tahoma" size="-1">This function offers data
source enumeration at runtime. It provides more flexibility than <b>SQLDriverConnect</b>
because you can call <b>SQLBrowseConnect</b> several times in succession,
each time prompting the user for more specific information until finally
you obtain the working connection string.</font></td>
</tr>
</table>
<p><font face="Tahoma" size="-1">I'll examine <font color="#FFFFCC"><b>SQLConnect</b></font>
first. In order to use<font color="#FFFFCC"><b> SQLConnect</b></font>, you need
to know about DSN. DSN stands for Data Source Name, a string that uniquely identifies
a data source. A DSN identifies a data structure that contains info on how to
connect to a specific data source. The info includes what ODBC driver to use
and which database to connect to. You create, modify and delete DSNs using 32-bit
ODBC Administrator in the control panel. </font></p>
<p><font face="Tahoma" size="-1">SQLConnect has the following syntax:</font></p>
<blockquote>
<pre><font face="Tahoma"><b><font color="#CCFFCC">SQLConnect proto ConnectionHandle:DWORD<br> pDSN:DWORD,<br> DSNLength:DWORD,<br> pUserName:DWORD,<br> NameLength:DWORD,<br> pPassword:DWORD,<br> PasswordLength:DWORD</font></b></font></pre>
</blockquote>
<ul>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">ConnectionHandle</font></b></font><font face="Tahoma" size="-1">.
The handle to the connection you want to use.</font></li>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">pDSN</font></b></font><font face="Tahoma" size="-1">.
Pointer to the DSN string.</font></li>
<li><font color="#FFCCFF"><b><font face="Tahoma" size="-1">DSNLength</font></b></font><font face="Tahoma" size="-1">.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -