15 20 6 Vol. 20 No. 6 2002 12 Journal of Southeast Guizhou National Teacher s College Dec. 2002 VC + + ODBC API ( 550025) [ ] ODBC SQL, ODBC. ODBC API, VC + + ODBC API, Windows ODBC API. [ ] ODBC ;Visual C + + ; ;Windows [ ] TP312 [ ] A [ ] 1002-6991(2002) 06-0015 - 05 Database programming in VC + + :applying ODBC API XU Hua - rong (Staff room of Computer Science,Department of Basic Science, Guizhou University, Guiyang 550025,China) Abstract: ODBC is a kind of programming interface used by SQL. The application of ODBC can avoid the complication that the programmers meet when they try to handle the Data Source. This essay provides a general programming method of ODBC API. It indicates the programming method and tips of Database application used by ODBC API in Windows environment through programming a simple class orienting to the application of Database in VC + + by applying ODBC API directly. Keywords :ODBC ;visual C + + ;database ;programming in windows 1 ODBC API Viaual C + + ODBC API,MFC ODBC,DAO,OLEDB,ADO.,,. ODBC(Open DataBase Connectivity),,ODBC API, API ODBC. ODBC,. ODBC,,,ODBC. 2 ODBC API 2. 1, SQLALLocHandle SQLAllocEnv.,, SQLAllocHandle SQLAllocConnect. # include sql. h SQLHENV m henv ; SQLAllocHandle(SQL HANDLE ENV,NULL, &m henv) ; SQLAllocEnv( &m henv) ; SQLAllocEnv SQLAllocConnect 1. 0 ODBC. ODBC3. 0, SQLAllocHandle,, SQLAllocEnv. 2,3 SQLAllocConnect SQLAllocStmt. [ ] 2002-07 - 28 [ ] (1976 - ),,,.
16 2002 12 2. 2, SQLAllocHandle SQLAllocConnect. SQLHDBC m hdbc ; SQLAllocHandle(SQL HANDLE DBC,m henv, &m hdbc) ; SQLAllocConnect(m henv, &m hdbc) ; 2. 3, ODBC SQL SQL,. SQLHSTMT m hmtst ; SQLAllocHandle(SQL HANDLE STMT,m hdbc, &m hstmt) ; SQLAllocStmt (m hdbc, &m hstmt) ; 2. 4 SQL SQLExecDirect SQLPrepare SQLExecute SQL. SQLExecDirect,SQLExecute SQL,, SQLPrepare, SQL SQLExecute. 2. 5 SQL,. SQL, SQLBindCol SQL, SQLFetch. SQLFetch,,,, SQLFetchscroll. 3 :, Access, ODBC.,Access db1 usrdb mysql. h id 9 # ifndef MYSQL H usename 10 grade 4 depaname 10 speciname 14 # define MYSQL H # include odbcinst. h class CMysql { CMysql () {Init () ; CMysql (CString strconn) {Init () ;Connect (strconn) ; ΠΠ CMysql () ; ΠΠ void Init (void) ; ΠΠ void Close(void) ; ΠΠ int ExeSql (LPCSTR) ; ΠΠ SQL void GetError(CString&) ΠΠ SQL int Connect (LPCSTR) ; ΠΠ ODBC void DataBindRequ(void) ; ΠΠ int Fetch(void) ; ΠΠ
6 :VC + + ODBC API 17 # define NAME LEN 10 # define CUSTID LEN 10 # define GRADE LEN 4 # define DEPA LEN 10 # define SPECI LEN 14 private : void DispError(void) ; ΠΠ SQL private : TUCHAR 3 m psql ; TUCHAR 3 m perrormsg ; SQLHENV m henv ; SQLHDBC m hdbc ; SQLHSTMT m hstmt ; ΠΠ SQL long int cbgrade,cbdepa,cbspeci,cbname,cbcustid ; char szgrade[ GRADE LEN],szDepa[DEPA LEN],szSpeci[ SPECI LEN] ; char szcustid[ CUSTID LEN],szName[NAME LEN] ; ΠΠ ; # endif mysql. cpp # include afx. h # include mysql. h # include < sql. h > # include < sqlext. h > # include < srv. h > CMysql : : CMysql () { void CMysql : : Init (void) { m perrormsg = 0 ; m psql = 0 ; m perrormsg = new TUCHAR[ SQL MAX MESSAGE LENGTH - 1 ] ; m hdbc = NULL ; m henv = NULL ; m hstmt = NULL ; ΠΠ SQL int CMysql : : ExeSql(LPCSTR strsql) { if ( m psql) { delete m psql ; if (m hstmt! = NULL) { Close () ; m psql = nev TUCHAR[ strlen(strsql) + 1 ] ; strcpy( (char 3 ) m psql,strsql) ; int sqlret = SQLExecDirect(m hstmt,m psql,sql NTS) ; if ( (sqlret!= SQL SUCCESS &&sqlret!= SQL SUCCESS WITH INFO) ) { DispError() ; return sqlret ; int CMysql : :Connect(LPCSTR strsrc)
18 2002 12 { int mconn = SQLAllocEnv( &m if (mconn = = SQL henv) : ΠΠ { mconn = SQLAllocConnect(m henv, &m hdbc) ; ΠΠ if (mconn = = SQL { SQLSetConnectOption(m hdbc,sql LOGIN TIMEOUT,5) ; ΠΠ TUCHAR 3 pstrsrc = new TUCHAR[ strlen(strsrc) + 1 ] ; TUCHAR 3 pstrusr = new TUCHAR ; TUCHAR 3 pstrpasswd = new TUCHAR ; strcpy( (char 3 ) pstrsrc,strsrc) ; mconn = SQLConnect(m hdbc,pstrsrc,sql NTS,pstrUsr,SQl NTS,pstrpasswd,SQL NTS) ; if ( (mconn = = SQL (mconn = = SQL SUCCESS WITH INFO) ) { mconn = SQLAllocStmt(m hdbc, &m hstmt) ; ΠΠ delete pstrsrc ; delete pstrusr ; delete pstrpasswd ; else { DispError() ; else { DispError() ; return mconn ; void CMysql : :DispError(void) { CString strerror ; GetError(strError) ; if (! strerror. IsEmpty() ) { MessageBox(NULL,strError,!,MB OK) ; void CMysql : : GetError(CString& strmsg) { TUCHAR 3 psqlsta = new TUCHAR[ SQL MAX MESSAGE LENGTH - 1 ] ; SDWORD Naerror ; SWORD strerrornsg ; SQLError(m henv,m hdbc,m hstmt,psqlsta, &Naerror,m perrormsg, SQL MAX MESSAGE LENGTH - 1, &strerrormsg) ; delete psqlsta ; strmsg = m perrormsg ; int CMysql : :Fetch(void) { int sqlret = SQLFetch(m hstmt) ; if ( (sqlret! = SQL SUCCESS &&sqlret! = SQL SUCCESS WITH INFO) ) DispError() ; return sqlret ; void CMysql : :Close(void) { if (m hstmt! = NULL) { SQLFreeStmt (m hstmt,sql DROp) ; ΠΠ
6 :VC + + ODBC API 19 SQLAllocStmt (m hdbc, &m hstmt) ; ΠΠ void CMysql : :DataBindRequ(void) ΠΠ { SQLBindCol (m hstmt,1,sql C CHAR,szCustID,CUSTID LEN, &cbcustid) ; SQLBindCol (m hstmt,2,sql C CHAR,szName,NAME LEN, &cbname) ; SQLBindCol (m hstmt,3,sql C CHAR,szGrade,GRADE LEN, &cbgrade) ; SQLBindCol (m hstmt,4,sql C CHAR,szDepa,DEPA LEN, &cbdepa) ; SQLBindCol (m hstmt,5,sql C CHAR,szSpeci,SPECI LEN, &cbspeci) ; # include mysql. cpp void main() { CMysql sqld( db1 ) ; ΠΠ if (sqld. ExeSql ( select id,usename,grade,depaname,speciname from usrdb ) = = SQL { sqld. DataBindRequ() ; while (sqld. Fetch() = = SQL { 4 ΠΠ printf ( %s, %s, %s, %s, %s \ n,sqld. szcustid,sqld. szname,sqld. szgrade,sqld. szdeps,sqld. szspeci) ; ΠΠ sqld. Close() ; ΠΠ, ODBC API., SQL, CMysql ExeSql,., ODBC, MFC CRecordSet. [ ] [1 ],. Visual C + + 6. 0 [M]. :,2000. [2 ] ( ) Gulutzan,P.,Pelzer,T.,. SQL - 3 [M]. :,2000. [3 ] ( ) Davidj. Kruglinski. Visual C + + ( ) [M]. :,1996.