Share via


Using OLE DB Error Objects (SQL Server Compact Edition)

When errors occur during the execution of a SQL Server 2005 Compact Edition (SQL Server Compact Edition)-based application, the OLE DB provider for SQL Server Compact Edition returns and stores an array of error objects. These objects can then be accessed using OLE DB in the typical manner. The OLE DB provider for SQL Server Compact Edition returns errors for each interface supported by the provider. For more information, see Implemented OLE DB Interfaces (SQL Server Compact Edition). For information about the general mechanism for an OLE DB client to retrieve error information, see the Microsoft OLE DB section of the Microsoft Data Access Components (MDAC) SDK documentation in the MSDN Library.

Examples

The following example shows how to retrieve the provider-specific error numbers when you use the OLE DB provider for SQL Server Compact Edition.

/* This code sample demonstrates a routine that can handle and display errors from the OLE DB provider for SQL Server Compact Edition. The interface returning the error is pIUnknown, and riid is the REFIID of that interface.*/

// QueryInterface for the ISupportErrorInfo interface
hr = pIUnknown->QueryInterface(IID_ISupportErrorInfo,
    (void**)&pISupportErrorInfo);
if(FAILED(hr) || NULL == pISupportErrorInfo)
    return;

// Determine whether the interface even supports errors. If it does,
// ISupportErrorInfo will return S_OK for that interface.
hr = pISupportErrorInfo->InterfaceSupportsErrorInfo(riid);
pISupportErrorInfo->Release(); // release unnecessary interface

if(S_OK == hr)
{
    // This interface supports returning error information.
    // Get the error object from the system for this thread.
    hr = GetErrorInfo(0, &pIErrorInfo);
    if(FAILED(hr) || NULL == pIErrorInfo)
    {
        return;
    }

    hr = pIErrorInfo->QueryInterface(IID_IErrorRecords,
        (void **) &pIErrorRecords);

    pIErrorInfo->Release();  // Release unnecessary interface

    // Determine the number of records in this error object
    hr = pIErrorRecords->GetRecordCount(&ulNumErrorRecs);

    // Loop over each error record in the error object to display 
    // information about each error. Errors are returned. 
    for (dwErrorIndex = 0; dwErrorIndex < ulNumErrorRecs; dwErrorIndex++) 
    {
        // Attempt to retrieve basic error information for this error.
        hr = pIErrorRecords->GetBasicErrorInfo(dwErrorIndex, &ErrorInfo);

        // Retrieve standard error information for this error.
        hr = pIErrorRecords->GetErrorInfo(dwErrorIndex, NULL,
            &pIErrorInfoRecord);

        // Get the description of the error.
        hr = pIErrorInfoRecord->GetDescription(&bstrDescriptionOfError); 

        // Get the source of the error.
        hr = pIErrorInfoRecord->GetSource(&bstrSourceOfError);

        if(NULL != pIErrorInfoRecord)
        {
            pIErrorInfoRecord->Release(); // Release unnecessary interface
        }

        // Print the native error number for this error. Error numbers are
        // are documented in the Troubleshooting section.
        wprintf(L"Native Error Code: %l\n", ErrorInfo.dwMinor);
    }

    pIErrorRecords->Release();  // Release unnecessary interface.
    }
}

See Also

Reference

OLE DB Provider Reference (SQL Server Compact Edition)

Help and Information

Getting SQL Server Compact Edition Assistance