使用 OLE DB 错误对象 (SQL Server Compact Edition)

在执行基于 SQL Server 2005 Compact Edition (SQL Server Compact Edition) 的应用程序期间发生错误时,OLE DB Provider for SQL Server Compact Edition 将返回并存储一组错误对象。然后,可以用通常的方式使用 OLE DB 来访问这些对象。OLE DB Provider for SQL Server Compact Edition 可以返回访问接口支持的每个接口的错误。有关详细信息,请参阅实现的 OLE DB 接口 (SQL Server Compact Edition)。有关 OLE DB 客户端检索错误信息的常规机制的信息,请参阅 MSDN Library 中 Microsoft 数据访问组件 (MDAC) SDK 文档的 Microsoft OLE DB 部分。

示例

下面的示例说明如何在使用 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.
    }
}

请参阅

参考

OLE DB for SQL Server Compact Edition 编程

帮助和信息

获取 SQL Server Compact Edition 帮助