マネージ アプリケーションでのエラー処理 (SQL Server Compact)

このトピックでは、SQL Server Compact 3.5 のデータ プロバイダーによって提供されるエラー オブジェクトの使用方法を示すコード例を紹介します。これらのオブジェクトを使用すると、Replication オブジェクト、RemoteDataAccess オブジェクト、または Engine オブジェクトの各メソッドの実行時に SQL Server Compact 3.5 で発生するエンジン エラーをキャプチャして表示できます。

SqlCeException オブジェクト

エンジン エラーが発生すると、SqlCeException オブジェクトが作成されます。この例外オブジェクトには、SqlCeErrorCollection オブジェクトが含まれます。このオブジェクトは、例外の各エラーに対して 1 つの SqlCeError オブジェクトを含むコレクションです。SqlCeErrorCollection オブジェクトには、SqlCeException.Errors プロパティを使用して直接アクセスできます。各 SqlCeError オブジェクトには、エラーに関する詳細情報を設定するエラー パラメータが含まれます。SQL Server と異なり、SQL Server Compact 3.5 はエラーに関する詳細情報をパラメータのコレクションとして返します。エラー メッセージを作成するときは、一連の FOR ループを入れ子にして、コレクション内の各 SqlCeError オブジェクトに含まれるパラメータを取得することをお勧めします。

次の例では、ShowSqlException メソッドで SQL Server Compact 3.5 エンジンの例外エラーをキャッチしています。この SqlCeException オブジェクトは、ShowErrors メソッドに渡されます。このメソッドは、SqlCeErrorCollection オブジェクトに含まれる各 SSCEError オブジェクトを表示します。このメソッドでは、各エラーのエラー パラメータすべてをループ処理します。

C#

// Reference the data provider.
using System.Data.SqlServerCe;

// Start the method to generate a database engine exception.
public void ShowSqlCeException() 
{
    string mySelectQuery = "SELECT column1 FROM table1";
    SqlCeConnection myConnection = new SqlCeConnection("Data Source=nonExistSource.sdf;");
    SqlCeCommand myCommand = new SqlCeCommand(mySelectQuery,myConnection);

    try 
    {
        myCommand.Connection.Open();
    }

    // Catch the exception as e and pass it to the ShowErrors routine.
    catch (SqlCeException e) 
    {
        ShowErrors(e);
    }

}

// Error handling routine that generates an error message
public static void ShowErrors(SqlCeException e) 
{
    SqlCeErrorCollection errorCollection = e.Errors;

    StringBuilder bld = new StringBuilder();
    Exception inner = e.InnerException;

    if (null != inner) 
    {
        MessageBox.Show("Inner Exception: " + inner.ToString());
    }
    // Enumerate the errors to a message box.
    foreach (SqlCeError err in errorCollection) 
    {
        bld.Append("\n Error Code: " + err.HResult.ToString("X")); 
        bld.Append("\n Message   : " + err.Message);
        bld.Append("\n Minor Err.: " + err.NativeError);
        bld.Append("\n Source    : " + err.Source);

        // Enumerate each numeric parameter for the error.
        foreach (int numPar in err.NumericErrorParameters) 
        {
            if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
        }

        // Enumerate each string parameter for the error.
        foreach (string errPar in err.ErrorParameters) 
        {
            if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);
        }

        MessageBox.Show(bld.ToString());
        bld.Remove(0, bld.Length);
    }
}

Visual Basic

' Reference the data provider by using the Imports directive.
Imports System.Data.SqlServerCe

' Start the method to generate a database engine exception.
Public Sub ShowSqlCeException()
    Dim mySelectQuery As String = "SELECT column1 FROM table1"
    Dim myConnection As New SqlCeConnection("Data Source=nonExistSource.sdf;")
    Dim myCommand As New SqlCeCommand(mySelectQuery, myConnection)

    Try
        myCommand.Connection.Open()

    ' Catch the exception as e and pass it to the ShowErrors routine.
    Catch e As SqlCeException

        ShowErrors(e)

    End Try
End Sub

' Error handling routine that generates an error message
Public Shared Sub ShowErrors(ByVal e As SqlCeException)
    Dim errorCollection As SqlCeErrorCollection = e.Errors

    Dim bld As New StringBuilder()
    Dim inner As Exception = e.InnerException

    If Not inner Is Nothing Then
        MessageBox.Show(("Inner Exception: " & inner.ToString()))
    End If

    Dim err As SqlCeError

    ' Enumerate each error to a message box.
    For Each err In errorCollection
        bld.Append((ControlChars.Cr & " Error Code: " & err.HResult.ToString("X")))
        bld.Append((ControlChars.Cr & " Message   : " & err.Message))
        bld.Append((ControlChars.Cr & " Minor Err.: " & err.NativeError))
        bld.Append((ControlChars.Cr & " Source    : " & err.Source))

        ' Retrieve the error parameter numbers for each error.
        Dim numPar As Integer
        For Each numPar In err.NumericErrorParameters
            If 0 <> numPar Then
                bld.Append((ControlChars.Cr & " Num. Par. : " & numPar))
            End If
        Next numPar

        ' Retrieve the error parameters for each error.
        Dim errPar As String
        For Each errPar In err.ErrorParameters
            If [String].Empty <> errPar Then
                bld.Append((ControlChars.Cr & " Err. Par. : " & errPar))
            End If
        Next errPar

        MessageBox.Show(bld.ToString())
        bld.Remove(0, bld.Length)
    Next err
End Sub

関連項目

その他の技術情報

スマート デバイス アプリケーションの構築 (SQL Server Compact)

デスクトップ用アプリケーションの構築 (SQL Server Compact)

Managed Data Provider (SQL Server Compact)

ネイティブ アプリケーションでのエラー処理