怎么用VC连接ACCESS数据库
1个回答
展开全部
_variant_t var;
CString strName,strAdd;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset)); try{m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\My Documents\\sjk.mdb",
"","",adModeUnknown);}catch(_com_error e){AfxMessageBox("数据库连接失败,确认数据库sjk.mdb是否在当前路径下!");
//return FALSE;} try{m_pRecordset->Open("SELECT * FROM biao",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);}catch(_com_error *e){AfxMessageBox(e->ErrorMessage());} try{if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();else{MessageBox("表内数据为空");
} while(!m_pRecordset->adoEOF){var = m_pRecordset->GetCollect("name");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("add");
if(var.vt != VT_NULL)
strAdd = (LPCSTR)_bstr_t(var); m_AccessList.AddString( strName + " --> "+strAdd ); m_pRecordset->MoveNext();
} m_AccessList.SetCurSel(0);}catch(_com_error *e){MessageBox(e->ErrorMessage());}m_pRecordset->Close();
m_pConnection->Close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询