#include <afxdb.h> //以提供对CDatabase 类的支持 #include <odbcinst.h> #pragma comment(lib,"odbccp32.lib") //为了使用ODBC API的函数 SQLConfigDataSource #include <odbcinst.h> CDatabase m_database; //创建数据源 SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,"Microsoft Access Driver (*.mdb)","DSN=数据\0""DBQ=C:\\数据.mdb\0"); ///打开数据库 if(!m_database.Open( "数据", FALSE, FALSE, _T("ODBC;UID=JOES"))) { AfxMessageBox( "连接失败 "); } //创建表 //CString strSQLexe="Create table test(iID int , tmJoin datetime, szName varchar(40),fTall float );"; //m_database.ExecuteSQL(strSQLexe); //查询操作 CRecordset *rs = new CRecordset(&m_database); CString lpszSQL= "select userName,userPass from admin"; if(rs-> Open(CRecordset::forwardOnly,lpszSQL)) { CDBVariant varValue; while(!rs->IsEOF()) { for(short index = 0; index < 2; index++) { rs->GetFieldValue(index, varValue); // do something with varValue AfxMessageBox(varValue.m_pstring->GetBuffer(2)); } rs->MoveNext(); } } rs->Close(); m_database.Close();