使用ADOX,需要引入ADOX的动态链接库msadox.dll,首先在stdafx.h中加入如下语句:
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
然后在程序初始化的时候需要初始化COM。在App::InitInstance()函数中加入
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
然后在程序初始化的时候需要初始化COM。在App::InitInstance()函数中加入
if( ! AfxOleInit())
{
AfxMessageBox( "OLE初始化出错!");
return FALSE;
}
然后进行连接数据库
// 定义ADO连接、命令、记录集变量指针
_ConnectionPtr m_pConnection;
_variant_t RecordsAffected;
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
_variant_t RecordsAffected;
_RecordsetPtr m_pRecordset;
try
{
m_pConnection . CreateInstance( __uuidof( Connection));
//连接SQL SERVER
//m_pConnection->Open("Driver=SQL Server;Database=test;Server=127.0.0.1;UID=sa;PWD=123;","","",adModeUnknown);
//连接ACCESS2003
m_pConnection -> Open(" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/UserInfo.mdb; Jet OLEDB:DataBase password=123" , "" , "" , adModeUnknown);
//m_pConnection->Open("Driver=SQL Server;Database=test;Server=127.0.0.1;UID=sa;PWD=123;","","",adModeUnknown);
//连接ACCESS2003
m_pConnection -> Open(" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/UserInfo.mdb; Jet OLEDB:DataBase password=123" , "" , "" , adModeUnknown);
}
catch( _com_error e)
{
CString errormessage;
errormessage . Format( "连接数据库失败! /r 错误信息:%s" , e . ErrorMessage());
AfxMessageBox( errormessage);
ExitProcess( 0);
}
连接成功之后向数据库执行查询语句select ALL* from userinfo,在按钮里添加如下代码。
void CUserDlg :: OnButtonAll()
{
// TODO: Add your control notification handler code here
m_list . DeleteAllItems();
try
{
m_pRecordset . CreateInstance( "ADODB.Recordset"); //为Recordset对象创建实例
_bstr_t strCmd = "select ALL* from userinfo"; //SQL语句
m_pRecordset = m_pConnection -> Execute( strCmd , & RecordsAffected , adCmdText); //执行
}
catch( _com_error & e)
{
AfxMessageBox( e . Description());
}
//SQL语句执行成功之后,以下的代码是获取记录返回的数据
_variant_t userid , username , u_groupid , u_tim , u_timed_tim , u_qq ,
u_email , u_realname , u_address , u_postcode , u_phone;
try
{
while( ! m_pRecordset -> adoEOF)
{
//获取返回的记录
userid = m_pRecordset -> GetCollect( "userid");
username = m_pRecordset -> GetCollect( "username");
u_groupid = m_pRecordset -> GetCollect( "u_groupid");
u_tim = m_pRecordset -> GetCollect( "u_tim");
u_timed_tim = m_pRecordset -> GetCollect( "u_timed_tim");
u_qq = m_pRecordset -> GetCollect( "u_qq");
u_email = m_pRecordset -> GetCollect( "u_email");
u_realname = m_pRecordset -> GetCollect( "u_realname");
u_address = m_pRecordset -> GetCollect( "u_address");
u_postcode = m_pRecordset -> GetCollect( "u_postcode");
u_phone = m_pRecordset -> GetCollect( "u_phone");
//对返回的用户信息记录插入m_list
if( userid . vt != VT_NULL)
{
m_list . InsertItem( 0 ,( LPCTSTR)( _bstr_t) userid);
}
if ( username . vt != VT_NULL)
{
m_list . SetItemText( 0 , 1 ,( LPCTSTR)( _bstr_t) username);
}
if ( u_groupid . vt != VT_NULL)
{
m_list . SetItemText( 0 , 2 ,( LPCTSTR)( _bstr_t) u_groupid);
}
if ( u_timed_tim . vt != VT_NULL)
{
m_list . SetItemText( 0 , 3 ,( LPCTSTR)( _bstr_t) u_tim);
}
if ( u_timed_tim . vt != VT_NULL)
{
m_list . SetItemText( 0 , 4 ,( LPCTSTR)( _bstr_t) u_timed_tim);
}
if ( u_qq . vt != VT_NULL)
{
m_list . SetItemText( 0 , 5 ,( LPCTSTR)( _bstr_t) u_qq);
}
if ( u_email . vt != VT_NULL)
{
m_list . SetItemText( 0 , 6 ,( LPCTSTR)( _bstr_t) u_email);
}
if ( u_realname . vt != VT_NULL)
{
m_list . SetItemText( 0 , 7 ,( LPCTSTR)( _bstr_t) u_realname);
}
if ( u_address . vt != VT_NULL)
{
m_list . SetItemText( 0 , 8 ,( LPCTSTR)( _bstr_t) u_address);
}
if ( u_postcode . vt != VT_NULL)
{
m_list . SetItemText( 0 , 9 ,( LPCTSTR)( _bstr_t) u_postcode);
}
if ( u_phone . vt != VT_NULL)
{
m_list . SetItemText( 0 , 10 ,( LPCTSTR)( _bstr_t) u_phone);
}
m_pRecordset -> MoveNext();
}
}
catch( _com_error & e)
{
AfxMessageBox( e . Description());
}
m_pRecordset -> Close(); //关闭记录对象
m_pRecordset = NULL;
}