代码如下:
BOOL CAlarmGuanliDIG::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//使用ADO创建数据库记录集
m_pRecordset.CreateInstance(__uuidof(Recordset)); // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误
try
{
m_pRecordset->Open("SELECT * FROM Alarm", // 查询s表中所有字段
theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}//增加数据的函数
void CAlarmGuanliDIG::OnAlarmadd()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_alarmid == "" || m_alarmname == "")
{
AfxMessageBox("报警ID和报警名不能为空!");
return;
}
//在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
//因为它有时会经常出现一些想不到的错误。
try
{
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("AlarmID", _variant_t(m_alarmid));
m_pRecordset->PutCollect("AlarmName", _variant_t(m_alarmname)); m_pRecordset->Update(); AfxMessageBox("插入成功!"); // 更新显示其库内容
int nCurSel = m_list.GetCurSel();
ListData();
m_list.SetCurSel(nCurSel);
// 移动记录指针到新的位置
OnSelchangeList1();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
报错如图
BOOL CAlarmGuanliDIG::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//使用ADO创建数据库记录集
m_pRecordset.CreateInstance(__uuidof(Recordset)); // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误
try
{
m_pRecordset->Open("SELECT * FROM Alarm", // 查询s表中所有字段
theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}//增加数据的函数
void CAlarmGuanliDIG::OnAlarmadd()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_alarmid == "" || m_alarmname == "")
{
AfxMessageBox("报警ID和报警名不能为空!");
return;
}
//在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
//因为它有时会经常出现一些想不到的错误。
try
{
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("AlarmID", _variant_t(m_alarmid));
m_pRecordset->PutCollect("AlarmName", _variant_t(m_alarmname)); m_pRecordset->Update(); AfxMessageBox("插入成功!"); // 更新显示其库内容
int nCurSel = m_list.GetCurSel();
ListData();
m_list.SetCurSel(nCurSel);
// 移动记录指针到新的位置
OnSelchangeList1();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
报错如图
This application has requested the Runtime to terminata it in an unusual way.Please contact the aplication's support team for more information.我选用try……catch()语句来捕捉错误信息,然而在添加和修改的时候总是会进入到catch语句当中,我在怀疑是不是程序错误了,但是有找不到原因在哪里,所以请高手来指教一下!谢谢!
if(!m_pRecordset->Supports(adAddNew)) return;
看看是不是可以避免, 不要忘了 做个异常判断