#include <afxwin.h>
#include <afxdao.h>void main()
{
CDaoDatabase db; AfxDaoInit();

CString strConn = "ODBC;Driver={SQL Server};"
"Server=MyServerName;Database=myDatabaseName;"
"Uid=myUsername;Pwd=myPassword"; CString strSQL, strName;
strSQL = "SELECT * FROM titles";
try
{
db.Open( "", FALSE, FALSE, strConn );
CDaoRecordset rs( &db ); rs.Open( dbOpenDynaset, strSQL, dbReadOnly );
COleVariant vName;
rs.GetFieldValue( "author", vName );
strName = vName.pcVal;
AfxMessageBox( "Found " + strName ); 
rs.Close();
db.Close();

}
catch ( CDaoException *e )
{
AfxMessageBox( e->m_pErrorInfo->m_strDescription,
MB_ICONEXCLAMATION );
e->Delete();

AfxDaoTerm();
}