在 Managed 應用程式中處理錯誤 (SQL Server Compact)
此主題提供了多個程式碼範例,這些範例將示範如何使用 SQL Server Compact 3.5 的資料提供者所提供的錯誤物件。在執行 Replication、RemoteDataAccess 或 Engine 物件方法時,這些物件可以用來擷取並顯示 SQL Server Compact 3.5 中發生的引擎錯誤。
SqlCeException 物件
發生引擎錯誤時,會建立 SqlCeException 物件。此例外狀況物件含有 SqlCeErrorCollection 物件,這接著會含有多個 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)