先安装MYSQL 的ODBC驱动,然后在系统的数据源当中添加好新的DSN
然后就可以在代码当中连接了:
try{
CDatabase db;
if(db.Open(NULL,FALSE,FALSE,"ODBC;DSN=mysqlDatabase;UID=root;PWD=********"))
{
MessageBox("Connection OK");
}else{
MessageBox("Connection failed");
}
}catch(CDBException* e)
{
AfxMessageBox(e->m_strError);
e->Delete();
}
获取结果集:
CRecordset m_set(&db);
m_set.Open( CRecordset::forwardOnly,("select * from Test"));
while(!m_set.IsEOF()){
CString name;
m_set.GetFieldValue("name",name);
MessageBox(name);
m_set.MoveNext();
}