直接复制过去修改就可以使用.要读读程序理解一下. //======================================================= _ConnectionPtr m_pConnection; //定义连接 CoInitialize(NULL);//初始化 m_pConnection.CreateInstance(__uuidof(Connection)); try { // 打开本地Access库db1.mdb m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown);//注意mdb1.mdb的保存位置 } catch(_com_error e) { AfxMessageBox("connect error"); } _RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance(__uuidof(Recordset)); try { m_pRecordset->Open("SELECT * FROM table1", m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针 dOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error *e) { AfxMessageBox("get recordset error"); } _variant_t var; CString str1,str2,str3; try { if(!m_pRecordset->BOF) m_pRecordset->MoveFirst();//移动到第一个记录 else { AfxMessageBox("no data"); } while(!m_pRecordset->adoEOF) { var=m_pRecordset->GetCollect("id"); if(var.vt != VT_NULL) str1= _com_util::ConvertBSTRToString((_bstr_t)var); var = m_pRecordset->GetCollect("name"); if(var.vt != VT_NULL) str2=_com_util::ConvertBSTRToString((_bstr_t)var); var = m_pRecordset->GetCollect("score"); if(var.vt != VT_NULL) str3=_com_util::ConvertBSTRToString((_bstr_t)var); AfxMessageBox(str1+"|"+str2+"|"+str3); m_pRecordset->MoveNext(); } } catch(_com_error *e) { AfxMessageBox("operate error"); } http://www.ednchina.com/blog/yoyowind/29532/message.aspx

评论