Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 155809 - Last Review: July 13, 2004 - Revision: 2.2
How To Use the SYS(3053) Function Handle with ODBC
This article was previously published under Q155809
Visual FoxPro 5.0 introduces a new SYS() function, SYS(3053), which returns
the ODBC hEnv (environment handle) that Visual FoxPro uses for connectivity
operations. One of the uses of the SYS() function is to get a list of
available datasources that users can connect to.
NOTE: Visual FoxPro uses the ODBC environment handle returned by SYS(3053)
for all of its connectivity operations. Use this handle for information
retrieval, not for changing options associated with it. In particular, you
should not call functions such as SQLFreeEnv(), which would release the
handle while FoxPro was using it and cause instability in the Visual FoxPro
environment.
The SYS(3053) return value is called an hEnv, which is a handle used by
ODBC to describe an environment in which connectivity operations take
place. One feature not available natively in FoxPro is the ability to
generate a list of datasources on users' machines.
ODBC has an API function called SQLDataSources that you can use to provide
this functionality. SQLDataSources requires an hEnv--which you can get from
the SYS(3053) function--to be passed to it. The sample below uses the
Visual FoxPro DECLARE (DLL) function to register the SQLDataSources API,
and then passes the value returned from SYS(3053) to that API in order to
retrieve the list of datasources and descriptions.
#DEFINE SQL_FETCH_NEXT 1
#DEFINE SQL_FETCH_FIRST 2
#DEFINE SQL_SUCCESS 0
#DEFINE MAX_STRING 128
DECLARE INTEGER SQLDataSources IN ODBC32.DLL ;
INTEGER henv, SHORT fdirection, ;
STRING @szDSN, INTEGER cbDSNMax, ;
INTEGER @pcbDSN, STRING @szDescription, ;
INTEGER cbDescriptionMax, INTEGER @pcbDescriptionn
hEnv = VAL(SYS(3053)) && The hEnv (converted to a number)
cbDsnMax = MAX_STRING && How long can the DSN Name be?
pcbDSN = 0 && How many were actually returned
cbDescriptMax = MAX_STRING && How long can the description be?
pcbDescript = 0 && How long the description actually was
fDirection = SQL_FETCH_FIRST && The first time start at the top
retVal = SQL_SUCCESS && Start with no errors
DO WHILE (retVal = SQL_SUCCESS)
szDsn = SPACE(MAX_STRING+1) && Make sure there is enough space
szDescript = SPACE(MAX_STRING+1)
retval = SQLDataSources(hEnv, fDirection, ;
@szDSN, cbDSNMax, @pcbDSN, ;
@szDescript, cbDescriptMax, @pcbDescript)
? LEFT(szDSN, pcbDSN)
? LEFT(szDescript, pcbDescript)
* We only want to do the SQL_FETCH_FIRST the first time
fDirection = SQL_FETCH_NEXT
ENDDO
APPLIES TO
- Microsoft Visual FoxPro 5.0 Standard Edition
- Microsoft Visual FoxPro 6.0 Professional Edition
- Microsoft Data Access Components 2.5
| kbautomation kbhowto kbinterop KB155809 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate