void CMyDlg::OnModify()
{
// TODO: Add your control notification handler code here
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try{
m_pRecordset->Open("SELECT * FROM starff",
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
m_pRecordset->MoveFirst();
m_pRecordset->Move(1);
m_pRecordset->PutCollect("员工号", atol("20"));
m_pRecordset->PutCollect("姓名", _variant_t("yyy"));
m_pRecordset->PutCollect("学历", _variant_t("yyy"));
m_pRecordset->PutCollect("工资", atol("200"));
m_pRecordset->Update();
MessageBox("修改第二条记录成功");
}
catch(_com_error e)
{
// MessageBox("对不起你没有连接数据库");
AfxMessageBox("对不起你没有连接数据库");
}
}
以上代码是我希望实现的意图是,当数据库没有进行连接的时候,那么m_pRecordset也就无法正常的打开表了
那么为什么不能实现catch中的函数功能呢?
throw 在哪?
m_pConnection.CreateInstance("ADODB.Connection");
if (m_pConnection == NULL)
{
TRACE("\nADO Connection 对象创建失败!");
return;
}
m_pRecordset.CreateInstance("ADODB.Recordset");
if (m_pRecordset == NULL)
{
TRACE("\nADO RecordSet 对象创建失败!");
return;
}
在这里,没必要用try...catch...,再说,也许捕捉到的不是com_error ,如果一定要用捕捉,那么改成catch(...)试一下。
呵呵
在VC中有两种形式:
TRY
{
}
CATCH(YouException e)
{
//do something
}
//这里用的是宏的形式
try
{
}
catch(YouException *e)
{
//do something
e->Delete();
}
//这是一般形式,记住一定要释放指正