为什么我的程序在调试的时候经常出现这样的错误,"Unhandled exception in XXXX.exe (xxxxxx.dll) 0xc0000005 Access Violation",还导致系统死机!!详情是,我用DAO操作数据库的时候,当在视图类操作数据库的时候不会有问题,但是当在其他自定义的类或者在文档类里执行到相关的数据库操作语句时(我的数据库用Access),就会弹出这样的对话框
"Unhandled exception in XXXX.exe (Dao350.dll) 0xc0000005 Access Violation"我是VC新手,这个问题看了很多书都不能解决,请各位大侠帮帮忙好吗?万分火急!!
"Unhandled exception in XXXX.exe (Dao350.dll) 0xc0000005 Access Violation"我是VC新手,这个问题看了很多书都不能解决,请各位大侠帮帮忙好吗?万分火急!!
解决方案 »
- vckbase不能免费下载资源了
- 关于ie6.0命令按钮问题
- WTL程序如何读写XML?有没有直接可用的库?
- acitvex 控件在ie 中的刷新问题
- CreateProcess() lpCmdLine参数中有带空格的长文件名
- 窗口最大化后任务栏无法弹出,这是为什么?
- 在被调用的程序中如何去掉菜单和工具栏
- 对话框类中没有m_bAutoMenuEnable数据成员怎样使EnableMenuItem函数起作用?!
- 用InsertColumn向ListView中添加了几列后,可运行时都跑到了左边看不见了,需拖动出来才看到,如何解决?
- 简单的问题,请问为什么CListCtrl的排序不正确
- 关于控件重定位的问题
- 高手看题: 绝对界面问题!
最好把相关的代码贴出来.
相关代码如下
问题出现在调用BOOL IsExistentTable(CDaoDatabase *pDatabase, CString strTableName)函数中的“pDatabase->GetTableDefInfo(strTableName, tableInfo); ”时候出现!!在视图类中调用不会出现问题!!
再次谢谢!!
/////////////////////////////////////////////////////
构造函数初始化数据库指针
CRead::CRead()
{
m_bWriten=FALSE;
Reading_error=0;
sequence=0;
m_hCom=theApp.m_hCom;
m_pDatabase= new CDaoDatabase;
m_pTableDef= new CDaoTableDef(m_pDatabase);
char path[50];
GetCurrentDirectory(50,path);
strcat(path,"\\");
m_strDatabaseName=_T(path);
m_strDatabaseName+=_T("Meters.mdb");//得到数据库文件的路径
if(openDatabase(&m_pDatabase,m_strDatabaseName,TRUE)!=1)
{
AfxMessageBox("数据库打开失败!");
return;
}
}
调用语句:
void CRead::ReadMeter(int tk,short Tn)
{
char mtrID[4];
itoa(Tn,mtrID,10);
BYTE Tp;
Tp=theApp.mtr[tk].m_nType;
switch(Tp)
{
case 0://EDMI表
m_strTableName="EDMI3_";
m_strTableName+=mtrID;
if(!IsExistentTable(m_pDatabase,m_strTableName))
{
if(!CreateNewTable(1,m_strTableName))
{
AfxMessageBox("动态创建数据表失败");
return;
}
}
hx3_Read(tk,Tn);
break;
case 1:
m_strTableName="EDMI6_";
m_strTableName+=mtrID;
if(!IsExistentTable(m_pDatabase,m_strTableName))
{
if(!CreateNewTable(1,m_strTableName))
{
AfxMessageBox("动态创建数据表失败");
return;
}
}
hx6_Read(tk,Tn);
break;
default:
break;
}
}
调用的两个数据库操作函数:
BOOL IsExistentTable(CDaoDatabase *pDatabase, CString strTableName)
{
if (pDatabase == NULL)
return FALSE;
BOOL bDuplicateTableName = TRUE;
CDaoTableDefInfo tableInfo; // only needed for the call
TRY
{ // this call will throw an exception if there is no
// table by the specified name--test for duplication
pDatabase->GetTableDefInfo(strTableName, tableInfo);
}
CATCH (CDaoException, e)
{
// if this is an 'Item not found' exception, do something
if (e->m_pErrorInfo->m_lErrorCode == 3265)
bDuplicateTableName = FALSE;
}
AND_CATCH (CMemoryException, e)
{
// do nothing
;
}
END_CATCH return bDuplicateTableName;
}int openDatabase(CDaoDatabase **ppDatabase, CString fileName,BOOL bReportNoOpen /* = TRUE */)
{
int nReturnCode = 1;
if (*ppDatabase != NULL)
{
if ((*ppDatabase)->IsOpen())
closeDatabase(ppDatabase);
delete *ppDatabase;
}
// construct new database
*ppDatabase = new CDaoDatabase;
if (ppDatabase == NULL)
return -1; // fatal error // now open the database object with error checking
try
{
(*ppDatabase)->Open(fileName);
}
catch (CDaoException *e)
{
if (e->m_pErrorInfo->m_lErrorCode == 3024)
{
if (bReportNoOpen)
{
// create a message to display
CString message = _T("Couldn't open database--Exception: ");
message += e->m_pErrorInfo->m_strDescription; // output status
AfxMessageBox(message);
}
nReturnCode = 0;
}
else // other type of DAO exception--always report
{
// create a message to display
CString message = _T("Couldn't open database--Exception: ");
message += e->m_pErrorInfo->m_strDescription;
AfxMessageBox(message);
nReturnCode = -1;
}
e->Delete();
delete *ppDatabase;
*ppDatabase = NULL;
}
catch (CMemoryException *e)
{
// output status
AfxMessageBox(_T("Failed to open database--Memory exception thrown."));
e->Delete(); delete *ppDatabase;
*ppDatabase = NULL;
nReturnCode = -1;
} return nReturnCode;
}
指针是空的。
也就是pDatabase->GetTableDefInfo(strTableName, tableInfo)中的
pDatabase是无效的指针。也可能参数是空的指针?你跟踪进去看看就应该知道了嘛