Created
June 10, 2012 14:04
-
-
Save cx20/2905782 to your computer and use it in GitHub Desktop.
Hello, ODBC(API) World!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <tchar.h> | |
#include <stdio.h> | |
#include <sqlext.h> | |
#include <sql.h> | |
int _tmain( int argc, TCHAR* argv[] ) | |
{ | |
HENV henv; | |
HDBC hdbc; | |
HSTMT hstmt; | |
_TCHAR szConnStr[1024]; | |
_TCHAR szConnOut[1024]; | |
SQLSMALLINT cchConOut; | |
_TCHAR szSQL[256]; | |
SQLTCHAR szColName[128]; | |
SQLSMALLINT nBufSize; | |
SQLSMALLINT nSqlType; | |
SQLUINTEGER nColSize; | |
SQLSMALLINT nScale; | |
SQLSMALLINT nNullable; | |
SQLTCHAR szMessage[256]; | |
SQLINTEGER nMessageLen; | |
SQLRETURN nReturn; | |
_tcscpy( szConnStr, _T("Driver={SQL Server};SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd") ); | |
_tcscpy( szSQL, _T("SELECT 'Hello, ODBC World!' AS Message") ); | |
SQLAllocEnv( &henv ); | |
SQLAllocConnect( henv, &hdbc ); | |
SQLDriverConnect( hdbc, NULL, (SQLTCHAR*)szConnStr, _tcslen(szConnStr), | |
(SQLTCHAR*)szConnOut, _countof(szConnOut), &cchConOut, SQL_DRIVER_NOPROMPT ); | |
SQLAllocStmt( hdbc, &hstmt ); | |
SQLExecDirect( hstmt, (SQLTCHAR*)szSQL, SQL_NTS ); | |
SQLDescribeCol( hstmt, 1, (SQLTCHAR*)szColName, _countof(szColName), | |
&nBufSize, &nSqlType, &nColSize, &nScale, &nNullable ); | |
SQLBindCol( hstmt, 1, SQL_C_TCHAR, szMessage, _countof(szMessage), &nMessageLen ); | |
while( TRUE ) | |
{ | |
nReturn = SQLFetch( hstmt ); | |
if( nReturn != SQL_SUCCESS | |
&& nReturn != SQL_SUCCESS_WITH_INFO ) | |
{ | |
break; | |
} | |
_tprintf( _T("%s\n"), szColName ); | |
_tprintf( _T("------------------\n") ); | |
_tprintf( _T("%s\n"), szMessage ); | |
} | |
SQLFreeStmt( hstmt, SQL_DROP ); | |
SQLDisconnect( hdbc ); | |
SQLFreeConnect( hdbc ); | |
SQLFreeEnv( henv ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment