我尝试下面这个程序段来给数据库添加一项,结果是:虽然编译通过,可以去到对话框的界面,但是当插入的时候,按插入键后弹出如下错误:
Unhandled exception in AdoRWAccess.exe(KERNEL32.DLL):0xE06D7363:Microsoft C++ Exception.
请问是什么错误,各位大侠可以帮我看看吗?
bool CAdoRWAccessDlg::OnInsert()
{
_ConnectionPtr m_pConnection;
// 初始化COM,创建ADO连接等操作m_pConnection.CreateInstance(__uuidof(Connection));// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Demo.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库Demo.mdb是否在当前路径下!");
return FALSE;
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM Demo", // 查询Demo表中所有字段
(IDispatch*)m_pConnection.GetInterfacePtr(), // 获取数据库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} try
{
// 写入各字段值
m_pRecordset->MoveLast();
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Name", _variant_t(m_Name));
m_pRecordset->PutCollect("Age", atol(m_Age));
m_pRecordset->Update(); AfxMessageBox("插入成功!");
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return FALSE;
} //关闭一个库连接、则用Close方法关闭它并赋于它空值
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
return FALSE;}
Unhandled exception in AdoRWAccess.exe(KERNEL32.DLL):0xE06D7363:Microsoft C++ Exception.
请问是什么错误,各位大侠可以帮我看看吗?
bool CAdoRWAccessDlg::OnInsert()
{
_ConnectionPtr m_pConnection;
// 初始化COM,创建ADO连接等操作m_pConnection.CreateInstance(__uuidof(Connection));// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Demo.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库Demo.mdb是否在当前路径下!");
return FALSE;
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM Demo", // 查询Demo表中所有字段
(IDispatch*)m_pConnection.GetInterfacePtr(), // 获取数据库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} try
{
// 写入各字段值
m_pRecordset->MoveLast();
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Name", _variant_t(m_Name));
m_pRecordset->PutCollect("Age", atol(m_Age));
m_pRecordset->Update(); AfxMessageBox("插入成功!");
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return FALSE;
} //关闭一个库连接、则用Close方法关闭它并赋于它空值
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
return FALSE;}
这句注释掉实验一下。
m_pRecordset= NULL;
断开连接之前
if(m_pRecordset!=NULL)
{
m_pRecordset->Close();
m_pRecordset.Release();
}
if(m_pConnection != NULL)
{
m_pConnection->Close();
m_pConnection.Release();
}
真想不明白是哪里出了问题。哪位有更进一步的建议吗?
//初始化COM
AfxOleInit();这句
其中bstrSQL是SQL语句,比如:insert into userinfo(...)(............
if(!AfxOleInit())//这就是初始化COM库
{
AfxMessageBox("OLE初始化出错!");
return FALSE;
} 我测试的查看和删除都可以,就是添加的时候出现了那样的错误。
这样写试试
Unhandled exception in AdoRWAccess.exe(KERNEL32.DLL):0xE06D7363:Microsoft C++ Exception.
真拿他没办法!
m_pRecordset->PutCollect("Name", _variant_t(m_Name));
m_pRecordset->PutCollect("Age", atol(m_Age));
我定义两个字符串变量,然后把编辑框的内容读入来,再把放到PutCollect中,这样就可以了。
CString strname,strage;
GetDlgItemText(IDC_EDIT_NAME,strname);
GetDlgItemText(IDC_EDIT_AGE,strage);
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Namefull", _variant_t(strname));
m_pRecordset->PutCollect("Agefull", _variant_t(atol(strage)));//
但是,随之而来的却有另外一个问题出现了。我的删除无法删除了。
明明列表框和数据库都没有空,当我选择一项来删除的时候,它总是提示我已经清空。看看我这样的删除程序是否有错:
try
{
CString strvoid;
GetDlgItemText(IDC_LIST1,strvoid);
int curSel = m_AccessList.GetCurSel();
if(strvoid.IsEmpty())
{
AfxMessageBox("您的列表框已经清空!");
} else
{
m_pRecordset->Delete(adAffectCurrent); // 参数adAffectCurrent为删除当前记录---数据库中的
m_pRecordset->Update();
m_AccessList.DeleteString(curSel);// 删除当前记录---列表框中的
AfxMessageBox("删除成功!");
} }
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
原来那样判断列表框是否是空是不行的。我采用下面的方式就可以了!
谢谢大家的帮助!
int curSel = m_AccessList.GetCurSel();
if(curSel == LB_ERR)
{
AfxMessageBox("您的列表框已经清空!");
}