SQL Server Compact Edition 오류 개체 사용

이 항목에서는 SQL Server 2005 Compact Edition(SQL Server Compact Edition)용 Microsoft .NET Compact Framework 데이터 공급자가 제공하는 오류 개체의 사용 방법을 보여 주는 코드 예제를 제공합니다. 이러한 개체를 사용하여 Replication, RemoteDataAccess 또는 Engine 개체 메서드를 실행할 때 SQL Server Compact Edition 에 발생하는 엔진 오류를 캡처 및 표시할 수 있습니다.

SqlCeException 개체

엔진 오류가 발생하면 SqlCeException 개체가 생성됩니다. 이 예외 개체에는 SqlCeErrorCollection 개체가 포함되어 있습니다. 이 개체에는 예외의 각 오류별로 SqlCeError 개체의 컬렉션이 포함되어 있습니다. SqlCeErrorCollection 개체는 SqlCeException.Errors 속성을 사용하여 직접 액세스할 수 있습니다. 각 SqlCeError 개체에는 오류에 대한 자세한 정보를 제공하는 오류 매개 변수 배열이 포함되어 있습니다. SQL Server 와 달리 SQL Server Compact Edition 에서는 매개 변수 컬렉션으로 오류에 대한 자세한 정보를 반환합니다. 오류 메시지를 작성할 때는 일련의 중첩된 FOR 루프를 사용하여 컬렉션의 각 SqlCeError 개체의 매개 변수를 검색하는 것이 좋습니다.

자세한 내용은 SQL Server Compact Edition .NET 프로그래밍를 참조하십시오.

다음 예에서는 ShowSqlException 메서드가 SQL Server Compact Edition 엔진 예외 오류를 catch합니다. 이 SqlCeException 개체는 ShowErrors 메서드로 전달됩니다. 그러면 SqlCeErrorCollection 개체의 각SSCEError 개체가 표시됩니다. 이 메서드는 각 오류에 대한 모든 오류 매개 변수에 걸쳐 반복 수행됩니다.

C#

// Reference the .NET Compact Framework Data Provider for SQL Server Compact Edition.
using System.Data.SqlServerCe;

// Start the method to generate a SQL Server Compact Edition 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  .NET Compact Framework Data Provider for SQL Server Compact Edition by using the Imports directive.
Imports System.Data.SqlServerCe

' Start the method to generate a SQL Server Compact Edition 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 Edition 지원 정보 보기