#include
#include
#include
#include
#include
#include
#include
#define MAXBUFLEN 255
#define MaxNameLen 20
//#define SQLBINDCOL
SQLHENV 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."<
}
// 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 "<
}
// Allocate an ODBC connection and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
if(retcode < 0 ) //错误处理
{
cout<<"allocate ODBC connection handle errors."<
}
//Data Source Name must be of type User DNS or System DNS
char* szDSN = "sqlhw";
char* szUID = "sa";//log name
char* szAuthStr = "wang";//passward
//connect to the Data Source
retcode=SQLConnect(hdbc1,(SQLCHAR*)szDSN,(SWORD)strlen(szDSN),(SQLCHAR*)szUID, (SWORD)strlen(szUID),(SQLCHAR*)szAuthStr, (SWORD)strlen(szAuthStr));
if(retcode < 0 ) //错误处理
{
cout<<"connect to ODBC datasource errors."<
}
// Allocate a statement handle.
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
if(retcode < 0 ) //错误处理
{
cout<<"allocate ODBC statement handle errors."<
}
// Execute an SQL statement directly on the statement handle.每一句后面都跟了一个错误处理,当发生错误时可以很方便的判断错在哪里
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"create table provider1(sno char(5) primary key,sname char(10) not null,status int,city char(10))", SQL_NTS);
if(retcode<0)
{
cout<<"creat errors."<
}
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S1','精益','20','天津')",SQL_NTS);
if(retcode<0)
{
cout<<"s1 insert errors."<
}
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S2','胜锡','10','北京')",SQL_NTS);
if(retcode<0)
{
cout<<"s2 insert errors."<
}
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S3','东方红','30','天津')",SQL_NTS);
if(retcode<0)
{
cout<<"s3 insert errors."<
}
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S4','丰泰盛','20','天津')",SQL_NTS);
if(retcode<0)
{
cout<<"s4 insert errors."<
}
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S5','为民','30','上海')",SQL_NTS);
if(retcode<0)
{
cout<<"s5 insert errors."<
}
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S6','通天','25',null)",SQL_NTS);
if(retcode<0)
{
cout<<"s6 insert errors."<
}
retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"SElECT sname,city FROM provider1", SQL_NTS);
if(retcode < 0 )
{
cout<<"Executing statement throught ODBC errors."<
}
// 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);
}