使用ADOX,需要引入ADOX的动态链接库msadox.dll,首先在stdafx.h中加入如下语句:
1 |
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF") |
然后在程序初始化的时候需要初始化COM。在App::InitInstance()函数中加入
1 2 3 4 5 |
if(!AfxOleInit()) { AfxMessageBox("OLE初始化出错!"); return FALSE; } |
然后进行连接数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
// 定义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; } |
转载请注明:exchen's blog » ADO 连接 ACCESS数据库