none
Checking ODBC errors from C# RRS feed

  • Question

  • Greetings:

     

    The function below was written for C++ ODBC error checking.  I'm trying to duplicate this functionality and I'm not sure whether I should reconstruct this or use some updated method.  I only know this function was extreemly helpful in troubleshooting ODBC errors and I don't want to lose any of its error reporting capability.  I just don't know whether there is a newer method or if I should stick with this and reconstruct it. 

     

    If it needs to be reconstructed, I definitely need help with it!

     

    Thanks!!

     

    // Helper function for debugging those cryptic ODBC errors.

    BOOL CHECK_ODBC_ERRORS(SQLSMALLINT HandType, SQLHANDLE HandValue, RETCODE rc, string FunctHeader, string &SQLState)

    {

     

    string FileName = "C:\\ErrorLog.txt";                               // file name for error messages

    string LogMessage;                                                       // holds the converted message to be written in the log

    const SQLINTEGER ErrorMsgMax = 1024;                     // total length allowed for any message

    SQLCHAR MsgTxt[ErrorMsgMax];                                 // stores messages from the SQL transaction

    SQLSMALLINT RecNum = 1;                                         // then 2, then 3....

    SQLCHAR State[20];                                                    // holds the SQL State or overall transaction info

    SQLINTEGER NativeError;                                             // holds the specific type of SQL error

    SQLSMALLINT MsgLen;                                                // indicates the length of messages

    bool NULLHandleError = false;                                       // assigned a value of true when HandValue type is determined

    char datebuf[9];                                                            // stores the date obtained from _strdate()

    char timebuf[9];                                                            // stores the time obtained from _strtime()

    char buffer[400];                                                           // holds the message which is converted into a string

    char buffer2[100];                                                         // temporary char storage for SQL state code

    int i;                                                                           // used as an index for 'buffer' array below

     

    _strdate(datebuf);                                                        // get current date

    _strtime(timebuf);                                                        // get current time

    if ( SQL_SUCCEEDED(rc) )

    return FALSE;                                                 // Skip if everything is ok.

    switch (HandType)

    {

    case SQL_HANDLE_ENV:

    if (HandValue == SQL_NULL_HENV)

    NULLHandleError = true;

    break;

    case SQL_HANDLE_DBC:

    if (HandValue == SQL_NULL_HDBC)

    NULLHandleError = true;

    break;

    case SQL_HANDLE_STMT:

    if (HandValue == SQL_NULL_HSTMT)

    NULLHandleError = true;

    break;

    }

    if (NULLHandleError){

    LogMessage = "SQLAllocHandle() failed to obtain a handle!\n";

    //WriteToFile(FileName, LogMessage);

    return false;

    }

    i = sprintf(buffer, FunctHeader.c_str());

    i += sprintf(buffer + i, " %s %s ********************\n", datebuf, timebuf);

    i += sprintf(buffer + i, "Return code = %d \n", rc);

    LogMessage = buffer;

    WriteToFile(FileName, LogMessage);

    for ( ; ; )

    {

    rc = SQLGetDiagRec(HandType, HandValue,RecNum, State, &NativeError,

    MsgTxt, ErrorMsgMax, &MsgLen);

    if ((rc != SQL_SUCCESS ) && (rc != SQL_SUCCESS_WITH_INFO) )

    break;


    sprintf(buffer2, "\nMsgTxt[%02d] SQLState '%s'\n", RecNum, State);

    sprintf(buffer2, "'%s'", State);

    SQLState = buffer2;

    i = sprintf(buffer, "\nMsgTxt[%02d] SQLState '%s'\n", RecNum, State);

    i += sprintf(buffer + i, "MsgTxt[%02d] NativeError %lu\n",RecNum, NativeError);

    i += sprintf(buffer + i, "MsgTxt[%02d] Message '%s'\n", RecNum, MsgTxt);

    LogMessage = buffer;

    WriteToFile(FileName, LogMessage);

    RecNum++;

    }


    if (RecNum == 1)

    {


    i = sprintf(buffer, "\nFirst call to SQLGetDiagRec failed, unidentified RETCODE = %d\n",rc);

    LogMessage = buffer;

    WriteToFile(FileName, LogMessage);

    if ( rc == SQL_NO_DATA_FOUND ){

    i = sprintf(buffer, "\nFirst call to SQLGetDiagRec failed with RETCODE");

    i += sprintf(buffer + i, " = SQL_NO_DATA_FOUND, See SQLError documentation\n");

    LogMessage = buffer;

    WriteToFile(FileName, LogMessage);

    }


    if ( rc == SQL_ERROR ){

    i = sprintf(buffer, "First call to SQLGetDiagRec failed with RETCODE");

    i += sprintf(buffer + i, " = SQL_ERROR, See SQLError documentation\n");

    LogMessage = buffer;

    WriteToFile(FileName, LogMessage);

    }

    if ( rc == SQL_INVALID_HANDLE ){

    i = sprintf(buffer, "First call to SQLGetDiagRec failed with RETCODE");

    i += sprintf(buffer + i," = SQL_INVALID_HANDLE, See SQLError documentation\n");

    LogMessage = buffer;

    WriteToFile(FileName, LogMessage);

    }

    }


    WriteToFile(FileName, "**************************** End Of ODBC Errors ***********************************\n\n" );

    return true;

    } // End of BOOL CHECK_ODBC_ERRORS()

    Tuesday, April 22, 2008 3:12 AM