次の方法で共有


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 クライアントでエラー情報を取得するための一般的なメカニズムについては、Microsoft Data Access Components (MDAC) SDK のドキュメントの「Microsoft OLE DB」セクションを参照してください。このドキュメントは、MSDN ライブラリで参照できます。

次の例は、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 のプログラミング (SQL Server Compact Edition)

ヘルプおよび情報

SQL Server Compact Edition のサポートについて