Freigeben über


Verwenden von OLE DB-Fehlerobjekten (SQL Server Compact Edition)

Wenn bei der Ausführung einer auf SQL Server 2005 Compact Edition (SQL Server Compact Edition) basierenden Anwendung Fehler auftreten, wird vom OLE DB-Anbieter für SQL Server Compact Edition ein Array von Fehlerobjekten zurückgegeben und gespeichert. Auf diese Objekte kann dann auf die übliche Weise mithilfe von OLE DB zugegriffen werden. Der OLE DB-Anbieter für SQL Server Compact Edition gibt Fehler für jede vom Anbieter unterstützte Schnittstelle zurück. Weitere Informationen finden Sie unter Implementierte OLE DB-Schnittstellen (SQL Server Compact Edition). Informationen zum allgemeinen Mechanismus für einen OLE DB-Client zum Abrufen von Fehlerinformationen finden Sie im Abschnitt über Microsoft OLE DB der Dokumentation für das Microsoft Data Access Components SDK (MDAC SDK) in der MSDN Library.

Beispiele

Das folgende Beispiel zeigt, wie die anbieterspezifischen Fehlernummern abgerufen werden können, wenn Sie den OLE DB-Anbieter für SQL Server Compact Edition verwenden.

/* 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.
    }
}

Siehe auch

Referenz

OLE DB-Programmierung für SQL Server Compact Edition

Hilfe und Information

Informationsquellen für SQL Server Compact Edition