#include <stdio.h>
#include <iostream>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>using namespace std;#define MAXBUFLEN   255
#define MaxNameLen  20//下面这句是预处理语句,使程序在编译时分别编译绑定段或SQLGetdata段
//#define SQLBINDCOLSQLHENV      henv = SQL_NULL_HENV;//定义环境句柄
SQLHDBC      hdbc1 = SQL_NULL_HDBC;//定义数据库连接句柄     
SQLHSTMT      hstmt1 = SQL_NULL_HSTMT;//定义语句句柄int main() 
{
RETCODE retcode;//错误返回码

    // Allocate the ODBC Environment and save handle.
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
if(retcode < 0 )//错误处理
{
cout<<"allocate ODBC Environment handle errors."<<endl;
return -1;
} // Notify ODBC that this is an ODBC 3.0 application.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
if(retcode < 0 ) //错误处理
{
cout<<"the  ODBC is not version3.0 "<<endl;
return -1;
}

// Allocate an ODBC connection and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
if(retcode < 0 ) //错误处理
{
cout<<"allocate ODBC connection handle errors."<<endl;
return -1;
} //Data Source Name must be of type User DNS or System DNS
char* szdDSN = "Test";
char* szUID = "sa";//log name
char* szAuthStr ="123";//passward
//connect to the Data SourceW
retcode=SQLConnect(hdbc1,(SQLWCHAR*)szdDSN,(SWORD)strlen(szdDSN),(SQLWCHAR*)szUID, (SWORD)strlen(szUID),(SQLWCHAR*)szAuthStr, (SWORD)strlen(szAuthStr));
if(retcode < 0 ) //错误处理
{
//******************************************程序在这里结束,下面代码可以先不看
cout<<"connect to  ODBC datasource errors."<<endl;
return -1;
} // Allocate a statement handle.
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
if(retcode < 0 ) //错误处理
{
cout<<"allocate ODBC statement handle errors."<<endl;
return -1;
}

// Execute an SQL statement directly on the statement handle.每一句后面都跟了一个错误处理,当发生错误时可以很方便的判断错在哪里
retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"select sname,city  FROM product", SQL_NTS);
if(retcode < 0 )
{
cout<<"Executing statement  throught ODBC  errors."<<endl;
return -1;
} // SQLBindCol variables
SQLCHAR      city[MaxNameLen + 1];
SQLCHAR  name[MaxNameLen + 1];
SQLINTEGER   columnLen = 0;//数据库定义中该属性列的长度#ifdef SQLBINDCOL

//游标已被封装在其中,一开始把两个列号分别写为provider中的列号2(name),4(city),结果name的值为city的值,city的值为烫烫烫烫烫烫烫烫,这才发现第二个参数应为游标中的列号,而不是表中的列号,
retcode = SQLBindCol(hstmt1, 1, SQL_C_CHAR,name,MaxNameLen , &columnLen); 
retcode = SQLBindCol(hstmt1, 2, SQL_C_CHAR,city,MaxNameLen , &columnLen);
while ( (retcode = SQLFetch(hstmt1) ) != SQL_NO_DATA) 
{
if(columnLen>0)
printf("name = %s  city = %s\n", name,city);
else
printf("name = %s  city = NULL\n", name,city);
}

#else

while(1 )
{
retcode = SQLFetch(hstmt1);
if(retcode == SQL_NO_DATA)
break;

retcode = SQLGetData(hstmt1, 1, SQL_C_CHAR, name, MaxNameLen, &columnLen);
retcode = SQLGetData(hstmt1, 2, SQL_C_CHAR, city, MaxNameLen, &columnLen);
if(columnLen>0)
printf("name = %s  city = %s\n", name,city);
else
printf("name = %s  city = NULL\n", name,city);

}#endif /* Clean up.*/
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);

return(0);
}