📄 oledberrorchecking.h
字号:
#ifndef Chucks_Error_Routine_Included
#define Chucks_Error_Routine_Included
#include <oledberr.h>
#include <atldbcli.h>
class COLEDBErrorChecking {
public:
//Inline functions
BOOL DBErrorsAreSupported(CComPtr<IUnknown> m_spUnk)
{
CComPtr<ISupportErrorInfo> spSupportErrorInfo;
if (SUCCEEDED(
m_spUnk->QueryInterface(IID_ISupportErrorInfo,
(void **) &spSupportErrorInfo))) {
if (SUCCEEDED(
spSupportErrorInfo->
InterfaceSupportsErrorInfo(IID_ICommandPrepare))){
return TRUE;
}
}
return FALSE;
}
void GetDBErrors(CComPtr<IUnknown> m_spUnk, char *msg)
{
CDBErrorInfo errInfo;
IErrorInfo *pErrorInfo = NULL;
BSTR pErrorDescription = NULL;
ULONG ulRecords = 0;
HRESULT hr =
errInfo.GetErrorRecords(m_spUnk,
IID_ICommandPrepare, &ulRecords);
if (FAILED(hr) || hr == S_FALSE || ulRecords == 0) {
//The error info object could not be retrieved
strcat(msg, "\n\nCould not retrieve an error info object.");
strcat(msg, "\nTherefore, additional error information is not available.");
}
else {
// Error info object was retrieved successfully
LCID lcid = GetUserDefaultLCID();
for (ULONG loop = 0; loop < ulRecords; loop ++) {
// Get the error information from the source
hr = errInfo.GetErrorInfo(loop, lcid, &pErrorInfo);
if (FAILED(hr)) {
continue;
}
//Get the error description
pErrorInfo->GetDescription(&pErrorDescription);
//Convert error description to single-width character
sprintf(msg, "%s\n\n%S", msg, pErrorDescription);
//Get SQLState and Error Code
GetSQLCodes(msg, &errInfo, loop);
//Clean up
SysFreeString(pErrorDescription);
pErrorInfo->Release();
}
}
}
void DisplayDBErrors(CComPtr<IUnknown> m_spUnk, char *msg)
{
//Allow 1 k for the error message
char message[1024];
if (msg)
strcpy(message, msg);
else
strcpy (message, "");
if (m_spUnk)
if (DBErrorsAreSupported(m_spUnk))
GetDBErrors(m_spUnk, message);
else
DisplaySingleError();
else
DisplaySingleError();
}
void DisplayAllErrors(CComPtr<IUnknown> m_spUnk,
char *msg = NULL,
HRESULT hresult = S_OK,
char *strFunction = NULL)
{
//Allow 1 k for the error message
char message[1024];
if (msg)
strcpy(message, msg);
else
strcpy (message, "");
if (strFunction) { //Check for function equal to null
//Function was passed, so see what function it was
strcat(message, " in the ");
strcat(message, strFunction);
strcat(message, " function ");
}
if (FAILED(hresult)) {
GetHRRESULTMessage(hresult, message);
}
DisplayDBErrors(m_spUnk, message);
}
//Static functions
static void DisplaySingleError(HRESULT hresult = S_OK, char *strFunction = NULL)
{
//Allow 1 k for the error message
char msg[1024];
strcpy (msg, "");
if (strFunction) { //Check for function equal to null
//Function was passed, so see what function it was
strcat(msg, "Error occurred in the ");
strcat(msg, strFunction);
strcat(msg, " function \n");
}
if (FAILED(hresult)) {
GetHRRESULTMessage(hresult, msg);
}
GetSingleError(msg);
::MessageBox(NULL, msg, "Error Message", MB_OK);
}
static void GetSingleError(char *msg)
{
IErrorInfo *pErrorInfo = NULL;
BSTR pErrorDescription = NULL;
if (SUCCEEDED(GetErrorInfo(0, &pErrorInfo))) {
//Get the error description
if (SUCCEEDED(pErrorInfo->GetDescription(&pErrorDescription))) {
//Convert error description to single-width character
sprintf(msg, "%s\n\n%S", msg, pErrorDescription);
//Clean up
SysFreeString(pErrorDescription);
}
else {
strcat(msg, "Could not find the error description");
}
pErrorInfo->Release();
}
else {
strcat(msg, "Could not retrieve error information");
}
}
static void GetSQLCodes(char *msg, CDBErrorInfo *errInfo, ULONG errorNum = 0)
{
//COM Error Interface to retrieve error codes
CComPtr<ISQLErrorInfo> spSQLErrorInfo;
char SQLState[100]; //Buffer for error message
if (SUCCEEDED(errInfo->GetCustomErrorObject(errorNum,
IID_ISQLErrorInfo,
(IUnknown**) &spSQLErrorInfo))) {
BSTR bstrSQLState = NULL; //SQLState that's returned
LONG errorCode; //SQL Code that's returned
//Retrieve the error code and SQLState
if (SUCCEEDED(spSQLErrorInfo->GetSQLInfo(&bstrSQLState, &errorCode))) {
//Form an error message
sprintf(SQLState,
"\n\nSQLState is %S\nError code is %ld",
bstrSQLState, errorCode);
//Concatenate the error message to the existing message
strcat(msg, SQLState);
SysFreeString(bstrSQLState); //Clean up
}
else {
strcat(msg,
"\n\nCould not get SQL info.");
}
}
else { //Something went wrong
strcat(msg,
"\n\nCould not get error or SQLState code.");
}
}
static void DisplayHRRESULTMessage(HRESULT hr, char *strFunction = NULL)
{
if (FAILED(hr)) {
//Allow 1 k for the error message
char message[1024];
strcpy (message, "");
if (strFunction) { //Check for function equal to null
//Function was passed, so see what function it was
strcat(message, "An Error occurred in the ");
strcat(message, strFunction);
strcat(message, " function \n\n");
}
GetHRRESULTMessage(hr, message);
::MessageBox(NULL, message, "Database HR Error", MB_OK);
}
}
static void GetHRRESULTMessage(HRESULT hr, char *msg)
{
sprintf(msg, "%s\nHRESULT was 0x%X:", msg, hr);
switch (hr) {
case DB_E_ABORTLIMITREACHED :
strcat(msg, "\nYour execution was aborted because a resource limit has been reached. No results are returned when this error occurs.");
break;
case DB_E_ALREADYINITIALIZED :
strcat(msg, "\nYou tried to initialize a data source that has already been initialized.");
break;
case DB_E_BADACCESSORFLAGS :
strcat(msg, "\nInvalid accessor flags");
break;
case DB_E_BADACCESSORHANDLE :
strcat(msg, "\nInvalid accessor handle");
break;
case DB_E_BADACCESSORTYPE :
strcat(msg, "\nThe specified accessor was not a parameter accessor");
break;
case DB_E_BADBINDINFO :
strcat(msg, "\nInvalid binding information");
break;
case DB_E_BADBOOKMARK :
strcat(msg, "\nInvalid bookmark");
break;
case DB_E_BADCHAPTER :
strcat(msg, "\nInvalid chapter");
break;
case DB_E_BADCOLUMNID :
strcat(msg, "\nInvalid column ID");
break;
case DB_E_BADCOMPAREOP :
strcat(msg, "\nThe comparison operator was invalid");
break;
case DB_E_BADCONVERTFLAG :
strcat(msg, "\nInvalid conversion flag");
break;
case DB_E_BADCOPY :
strcat(msg, "\nErrors were detected during a copy");
break;
case DB_E_BADDYNAMICERRORID :
strcat(msg, "\nThe supplied DynamicErrorID was invalid");
break;
case DB_E_BADHRESULT :
strcat(msg, "\nThe supplied HRESULT was invalid");
break;
// case DB_E_BADID :
// DB_E_BADID is deprecated.
// Use DB_E_BADTABLEID instead.
// break;
case DB_E_BADLOCKMODE :
strcat(msg, "\nInvalid lock mode");
break;
case DB_E_BADLOOKUPID :
strcat(msg, "\nInvalid LookupID");
break;
case DB_E_BADORDINAL :
strcat(msg, "\nThe specified column number does not exist.");
break;
case DB_E_BADPARAMETERNAME :
strcat(msg, "\nThe given parameter name is not recognized.");
break;
case DB_E_BADPRECISION :
strcat(msg, "\nA specified precision is invalid");
break;
case DB_E_BADPROPERTYVALUE :
strcat(msg, "\nThe value of a property is invalid");
break;
case DB_E_BADRATIO :
strcat(msg, "\nInvalid ratio");
break;
case DB_E_BADRECORDNUM :
strcat(msg, "\nThe specified record number is invalid");
break;
case DB_E_BADROWHANDLE :
strcat(msg, "\nInvalid row handle. This error often occurs when you are at BOF or EOF of a rowset and you try to update your data set.");
break;
case DB_E_BADSCALE :
strcat(msg, "\nA specified scale was invalid");
break;
case DB_E_BADSOURCEHANDLE :
strcat(msg, "\nInvalid source handle");
break;
case DB_E_BADSTARTPOSITION :
strcat(msg, "\nThe rows offset specified would position you before the beginning or past the end of the rowset.");
break;
case DB_E_BADSTATUSVALUE :
strcat(msg, "\nThe specified status flag was neither DBCOLUMNSTATUS_OK nor DBCOLUMNSTATUS_ISNULL");
break;
case DB_E_BADSTORAGEFLAG :
strcat(msg, "\nOne of the specified storage flags was not supported");
break;
case DB_E_BADSTORAGEFLAGS :
strcat(msg, "\nInvalid storage flags");
break;
case DB_E_BADTABLEID :
strcat(msg, "\nInvalid table ID");
break;
case DB_E_BADTYPE :
strcat(msg, "\nA specified type was invalid");
break;
case DB_E_BADTYPENAME :
strcat(msg, "\nThe given type name was unrecognized");
break;
case DB_E_BADVALUES :
strcat(msg, "\nInvalid value");
break;
case DB_E_BOOKMARKSKIPPED :
strcat(msg, "\nAlthough the bookmark was validly formed, no row could be found to match it");
break;
case DB_E_BYREFACCESSORNOTSUPPORTED :
strcat(msg, "\nBy reference accessors are not supported by this provider");
break;
case DB_E_CANCELED :
strcat(msg, "\nThe change was canceled during notification; no columns are changed");
break;
case DB_E_CANNOTRESTART :
strcat(msg, "\nThe rowset was built over a live data feed and cannot be restarted.");
break;
case DB_E_CANTCANCEL :
strcat(msg, "\nThe executing command cannot be canceled.");
break;
case DB_E_CANTCONVERTVALUE :
strcat(msg, "\nA literal value in the command could not be converted to the correct type due to a reason other than data overflow.");
break;
case DB_E_CANTFETCHBACKWARDS :
strcat(msg, "\nThe rowset does not support backward scrolling.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -