1. 程式人生 > >C++ODBC連線資料庫

C++ODBC連線資料庫

最近學習ODBC api連線oracle資料庫,這是找到的比較靠譜的底層程式碼,經過封裝可以使用

#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);
}

原文地址http://blog.csdn.net/perfect2011/article/details/4777648 

另我是在vs2013中編譯,需要將#include <iostream.h>改為#include <iostream>,同時新增using namespace std;