博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Exceptions: Database Exceptions (MSDN)
阅读量:6463 次
发布时间:2019-06-23

本文共 4753 字,大约阅读时间需要 15 分钟。

This article explains how to handle database exceptions. Most of the material in this article applies whether you are working with the MFC classes for Open Database Connectivity (ODBC) or the MFC classes for Data Access Objects (DAO). Material specific to one or the other model is explicitly marked. Topics include:


The approach is the same whether you are working with DAO or ODBC.

You should always write exception handlers to handle exceptional conditions.

The most pragmatic approach to catching database exceptions is to test your application with exception scenarios. Determine the likely exceptions that might occur for an operation in your code, and force the exception to occur. Then examine the trace output to see what exception is thrown, or examine the returned error information in the debugger. This lets you know which return codes you'll see for the exception scenarios you are using.

Error Codes Used for ODBC Exceptions

In addition to return codes defined by the framework, which have names of the form AFX_SQL_ERROR_XXX, some are based on return codes. The return codes for such exceptions have names of the form SQL_ERROR_XXX.

The return codes — both framework-defined and ODBC-defined — that the database classes can return are documented under the data member of class CDBException. Additional information about return codes defined by ODBC is available in the ODBC SDK Programmer's Reference in the MSDN Library.

Error Codes Used for DAO Exceptions

For DAO exceptions, more information is typically available. You can access error information through three data members of a caught object:

  • contains a pointer to a object that encapsulates error information in DAO's collection of error objects associated with the database.

  • contains an extended error code from the MFC DAO classes. These error codes, which have names of the form AFX_DAO_ERROR_XXX, are documented under the data member in CDaoException.

  • contains an OLE SCODE from DAO, if applicable. You'll seldom need to work with this error code, however. Usually more information is available in the other two data members. See the data member for more about SCODE values.

Additional information about DAO errors, the DAO Error object type, and the DAO Errors collection is available under class .


The following example attempts to construct a -derived object on the heap with the new operator, and then open the recordset (for an ODBC data source). For a similar example for the DAO classes, see "DAO Exception Example" below.

ODBC Exception Example

The member function could throw an exception (of type for the ODBC classes), so this code brackets the Open call with a try block. The subsequent catch block will catch a CDBException. You could examine the exception object itself, called e, but in this case it is enough to know that the attempt to create a recordset has failed. The catch block displays a message box and cleans up by deleting the recordset object.

C++
Copy
CRecordset* CMyDatabaseDoc::GetRecordset(){   CCourses* pSet = new CCourses(&m_dbCust);   try   {      pSet->Open();   }   catch(CDBException* e)   {      AfxMessageBox(e->m_strError, MB_ICONEXCLAMATION);      // Delete the incomplete recordset object      delete pSet;      pSet = NULL;      e->Delete();   }   return pSet;}

DAO Exception Example

The DAO example is similar to the example for ODBC, but you can typically retrieve more kinds of information. The following code also attempts to open a recordset. If that attempt throws an exception, you can examine a data member of the exception object for error information. As with the previous ODBC example, it is probably enough to know that the attempt to create a recordset failed.

C++
Copy
CDaoRecordset* CMyDaoDatabaseDoc::GetRecordset(){   CDaoRecordset* pSet = new CCustSet(&m_db);   try   {      pSet->Open();   }   catch(CDaoException* pe)   {      AfxMessageBox(pe->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);      // Delete the incomplete recordset object      delete pSet;      pSet = NULL;      pe->Delete();   }   return pSet;}

This code gets an error message string from the member of the exception object. MFC fills this member when it throws the exception.

For a discussion of the error information returned by a CDaoException object, see classes and .

When you are working with Microsoft Jet (.mdb) databases, and in most cases when you are working with ODBC, there will be only one error object. In the rare case when you are using an ODBC data source and there are multiple errors, you can loop through DAO's Errors collection based on the number of errors returned by . Each time through the loop, call to refill the m_pErrorInfo data member.

转载于:https://www.cnblogs.com/MarvinGeng/archive/2012/07/09/2582251.html

你可能感兴趣的文章
Tiny语言执行环境TM机源码
查看>>
PE文件之资源讲解
查看>>
windows 7/mac编译cocos2d-x-3.2*的android工程报错
查看>>
MYSQL导入导出.sql文件(转)
查看>>
使用Elasticsearch、Logstash、Kibana与Redis(作为缓冲区)对Nginx日志进行收集(转)
查看>>
git review报错一例
查看>>
Tomcat在Linux上的安装与配置
查看>>
《信息安全系统设计基础》 课程教学
查看>>
Linux平台下使用rman进行oracle数据库迁移
查看>>
全栈工程师学习Linux技术的忠告
查看>>
iOS自定制tabbar与系统的tabbar冲突,造成第一次点击各个item图片更换选中,第二次选中部分item图片不改变...
查看>>
C# Dictionary用法总结
查看>>
SVN服务器使用(二)
查看>>
反射获取内部类以及调用内部类方法
查看>>
C语言 - pthread
查看>>
谈Linq To Sql的优劣--纯个人观点
查看>>
HDU 4996 Revenge of LIS(DP)
查看>>
App里面如何正确显示用户头像
查看>>
DATAGUARD维护:从库宕机后如何恢复到管理恢复模式
查看>>
Android中的PID和UID
查看>>