用VS2005(MFC)读串口保存至ACCESS数据库出错,求高手赐教

leoric111 2011-11-16 11:35:47
数据库单独写INSERT和查询基本没有问题,把代码加到我串口接收线程的程序中就出现这个问题,求高手赐教!谢谢
下面该写的我都写了


#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace\
rename("EOF","adoEOF") rename("BOF","adoBOF")

_RecordsetPtr pHandlerRecordset;
_CommandPtr m_pCmd;
_ConnectionPtr m_pConnection;
_variant_t Reffected;
//申明


AfxOleInit();//或者CoInitialize(NULL);
//初始化Com接口创建Recordset和Connection指针
m_pConnection.CreateInstance("ADODB.Connection");
pHandlerRecordset.CreateInstance("ADODB.Recordset");

m_pConnection.CreateInstance(__uuidof(Connection));

try
{
// 打开本地Access库Info.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BDMData.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(_T("数据库连接失败,确认数据库BDMData.mdb是否在当前路径下!"));
// return FALSE;
}

下面是增加代码


void Cdb::OnAddnew(CString iTime,CString iRevData)
{
CString tempstr=_T("insert into SrcInfo (Time,RevData) values ('");
tempstr +=iTime;
tempstr +="','";
tempstr +=iRevData;
tempstr +="')";
try
{
m_pConnection->Execute((_bstr_t)tempstr,&Reffected,adCmdText);
pHandlerRecordset=m_pConnection->Execute("select * from BaseInfo",&Reffected,adCmdText); //更新RecordsetPtr
}
catch(_com_error e)
{
CString strError;
strError.Format(_T("%s\n%s\n%s"),e.Source(),(LPCSTR)e.ErrorMessage(),e.Description());
AfxMessageBox(strError);

}

}
问题出在m_pConnection->Execute((_bstr_t)tempstr,&Reffected,adCmdText);
这一句会报出_com_error异常,断点会跳至
inline _RecordsetPtr Connection15::Execute ( _bstr_t CommandText, VARIANT * RecordsAffected, long Options ) {
struct _Recordset * _result = 0;
HRESULT _hr = raw_Execute(CommandText, RecordsAffected, Options, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _RecordsetPtr(_result, false);
这一段中的if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
然后结束。
我单独写插入是正常,把代码加到我串口接收线程的程序中就出现这个问题,http://download.csdn.net/detail/leoric111/3803852源码已经传至这了,
求高手赐教!谢谢



...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyq5945 2011-11-17
  • 打赏
  • 举报
回复
INSERT语句可能没有拼写对,最好是在程序中下断点把SQL语句提取出来测试下。
leoric111 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zyq5945 的回复:]

C/C++ code
try
{
// 你的ADO代码
}
catch(_com_error& e)
{
CString strError;
strError.Format(_T("%s\n%s\n%s"),(LPCTSTR)e.Source(),(LPCTSTR)e.ErrorMessage(),(LPCTSTR)e.Description());
AfxMessageBox(str……
[/Quote]我都有捕获啊,没有catch到都是直接报错的
leoric111 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zyq5945 的回复:]

C/C++ code
try
{
// 你的ADO代码
}
catch(_com_error& e)
{
CString strError;
strError.Format(_T("%s\n%s\n%s"),(LPCTSTR)e.Source(),(LPCTSTR)e.ErrorMessage(),(LPCTSTR)e.Description());
AfxMessageBox(str……
[/Quote]我都有捕获啊,没有catch到都是直接报错的
zyq5945 2011-11-17
  • 打赏
  • 举报
回复
try
{
// 你的ADO代码
}
catch(_com_error& e)
{
CString strError;
strError.Format(_T("%s\n%s\n%s"),(LPCTSTR)e.Source(),(LPCTSTR)e.ErrorMessage(),(LPCTSTR)e.Description());
AfxMessageBox(strError);

}

所有的ADO代码都加异常捕获。

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧