数据库访问与查询

#include <afxwin.h>
#include <afxdb.h>
 
int _tmain(int argc,TCHAR *argv[],TCHAR *envp[])
{
AfxWinInit(GetModuleHandle(NULL),NULL,GetCommandLine(),0);
CDatabase db;
try
{
db.OpenEx("DSN=user1;",CDatabase::noOdbcDialog);
 
}catch(CDBException *pDBEx)
{
pDBEx->ReportError();
 
}catch(CMemoryException *pMemEx)
{
pMemEx->ReportError();
}
CRecordset rs(&db);
 
 
rs.Open(CRecordset::forwardOnly,"select * from user;",CRecordset::readOnly);
while(!rs.IsEOF())
{CString s;
rs.GetFieldValue("name",s);
rs.MoveNext();
AfxMessageBox(s);
}
 
rs.Close();
db.Close();
return 0;
}
原文地址:https://www.cnblogs.com/boobuy/p/2933334.html