VC访问ACeess

来源:百度知道 编辑:UC知道 时间:2024/05/09 11:40:57
建立了一个组合框,用于显示ACeess中的数据,在button按钮中添加了如下代码:
_ConnectionPtr m_pConnection;
AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
UpdateData();
try

m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://new.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库new.mdb是否在当前路径下!");

}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));

try
{
m_pRecordset->Open("SELECT * FROM data",_variant_t((IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}

variant_t var1,var2,var3,var4,var5;
CString strNumber,strBrand,strModel,strFactory,strDate;
try
{
if(!m_pRecordset->BOF)
{

m_

不要把数据库连接语句放到Btn的响应事件中,最好放到窗体的初始化函数中。多次连接肯定会报错的。

注意不要多次调用_ConnectionPtr _RecordsetPtr的open 和close方法.
==================================
try
{
if(!m_pRecordset->BOF)
{

m_pRecordset->MoveFirst();
}
else

{
AfxMessageBox("表内数据为空");
return;
}
这段搞不懂在干嘛
=================================
if(var1.vt != VT_NULL||var2.vt != VT_NULL||var3.vt != VT_NULL||var4.vt != VT_NULL||var5.vt != VT_NULL)
这里是不是应该把||改成&&

m_pRecordset->Close();
m_pRecordset->Release();
m_pConnection->Close();
m_pConnection->Release();