C++ODBC连接数据库

举报
香菜聊游戏 发表于 2021/07/15 02:42:32 2021/07/15
【摘要】  #include <stdio.h>#include <iostream.h>#include <string.h>#include <windows.h>#include <sql.h>#include <sqlext.h>#include <odbcss.h> #define...

 #include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>

#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."<<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* 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."<<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,(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."<<endl;
  return -1;
 }        
 
 retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S1','精益','20','天津')",SQL_NTS);
 if(retcode<0)
 {
  cout<<"s1 insert errors."<<endl;
  return -1;
 }

 retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S2','胜锡','10','北京')",SQL_NTS);
 if(retcode<0)
 {
  cout<<"s2 insert errors."<<endl;
  return -1;
 }

 retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S3','东方红','30','天津')",SQL_NTS);
 if(retcode<0)
 {
  cout<<"s3 insert errors."<<endl;
  return -1;
 }

 retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S4','丰泰盛','20','天津')",SQL_NTS);
 if(retcode<0)
 {
  cout<<"s4 insert errors."<<endl;
  return -1;
 }

 retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S5','为民','30','上海')",SQL_NTS);
 if(retcode<0)
 {
  cout<<"s5 insert errors."<<endl;
  return -1;
 }

 retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"insert into provider1 values('S6','通天','25',null)",SQL_NTS);
 if(retcode<0)
 {
  cout<<"s6 insert errors."<<endl;
  return -1;
 }

 retcode = SQLExecDirect(hstmt1,(SQLCHAR*)"SElECT sname,city  FROM provider1", 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);
}

文章来源: gamwatcher.blog.csdn.net,作者:香菜聊游戏,版权归原作者所有,如需转载,请联系作者。

原文链接:gamwatcher.blog.csdn.net/article/details/4777648

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。