Re: How to get ODBC the status of connection?



On Tue, 07 Aug 2007 23:10:17 -0700, Hooyoo <zhaohuyong@xxxxxxxxx>
wrote:

Why does it always return 0 whether the DB server is running or not?
My code looks like:
bool ODBCWrapper::IsConnected()
{
SQLINTEGER isConnected = 0, length = 0;
retcode_ = SQLGetConnectAttr(hdbc_, SQL_ATTR_CONNECTION_DEAD,
&isConnected,
sizeof(SQLINTEGER),&length);
if(retcode_ == SQL_SUCCESS || retcode_ == SQL_SUCCESS_WITH_INFO)
{
return (isConnected != 0); //It always is 0. Why ?????????
}
else
{
return false;
}
}

Could it be what you pass as the BufferLength argument? According to
the ODBC docs:

<quote>
BufferLength

[Input]
If Attribute is an ODBC-defined attribute and ValuePtr points to a
character string or a binary buffer, this argument should be the
length of *ValuePtr. If Attribute is an ODBC-defined attribute and
*ValuePtr is an integer, BufferLength is ignored. If the value in
*ValuePtr is a Unicode string (when calling SQLGetConnectAttrW), the
BufferLength argument must be an even number.


If Attribute is a driver-defined attribute, the application indicates
the nature of the attribute to the Driver Manager by setting the
BufferLength argument. BufferLength can have the following values:

If *ValuePtr is a pointer to a character string, then BufferLength is
the length of the string or SQL_NTS.


If *ValuePtr is a pointer to a binary buffer, then the application
places the result of the SQL_LEN_BINARY_ATTR(length) macro in
BufferLength. This places a negative value in BufferLength.


If *ValuePtr is a pointer to a value other than a character string or
binary string, then BufferLength should have the value
SQL_IS_POINTER.


If *ValuePtr contains a fixed-length data type, then BufferLength is
either SQL_IS_INTEGER or SQL_IS_UINTEGER, as appropriate.
</quote>

See if it works when writing the function call like this:

retcode_ = SQLGetConnectAttr(hdbc_, SQL_ATTR_CONNECTION_DEAD,
&isConnected, SQL_IS_POINTER, &length);

(in sqlext.h):
#define SQL_IS_POINTER (-4)

The size argument is only needed when writing to a variable-length
data type; however, the ODBC library often needs to be told whether
something is a value or a pointer.

Also, it looks like you don't actually check the return code for a
possible error? It is probably just falling through to the "else"
part of the if-block...

--
Bob Hairgrove
NoSpamPlease@xxxxxxxx
.