本主题提供了几个代码示例,说明了如何使用 SQL Server 2005 Compact Edition (SQL Server Compact Edition) 的 Microsoft .NET Compact Framework 数据访问接口所提供的错误对象。在执行 Replication、RemoteDataAccess 或 Engine 对象方法时,您可以使用这些对象捕获和显示 SQL Server Compact Edition 中发生的引擎错误。
SqlCeException 对象
当出现引擎错误时,将创建一个 SqlCeException 对象。此异常对象包含 SqlCeErrorCollection 对象。而这个对象又包含了 SqlCeError 对象的一个集合,每个对象分别对应异常中的一个错误。使用 SqlCeException.Errors 属性可以直接访问 SqlCeErrorCollection 对象。每个 SqlCeError 对象包含一个错误参数数组,该数组可提供详细错误信息。与 SQL Server 不同,SQL Server Compact Edition 将详细错误信息作为一个参数集合返回。在生成错误消息时,建议您使用一系列嵌套的 FOR 循环来检索集合内各个 SqlCeError 对象中的各个参数。
有关详细信息,请参阅SQL Server Compact Edition .NET 编程。
示例
在下面的示例中,ShowSqlException 方法捕获 SQL Server Compact Edition 引擎异常错误。此 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