ADO连接ACCESS数据库

使用ADOX,需要引入ADOX的动态链接库msadox.dll,首先在stdafx.h中加入如下语句:

#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;

  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);

}
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;    

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值