SqlErrorCollection.Count 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得集合中的錯誤數目。
public:
property int Count { int get(); };
public int Count { get; }
member this.Count : int
Public ReadOnly Property Count As Integer
屬性值
集合中的錯誤總數。
實作
範例
下列範例會顯示集合內的每一 SqlError 個 SqlErrorCollection 。
public static void ShowSqlException(string connectionString)
{
string queryString = "EXECUTE NonExistantStoredProcedure";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
DisplaySqlErrors(ex);
}
}
}
private static void DisplaySqlErrors(SqlException exception)
{
for (int i = 0; i < exception.Errors.Count; i++)
{
Console.WriteLine("Index #" + i + "\n" +
"Error: " + exception.Errors[i].ToString() + "\n");
}
Console.ReadLine();
}
Public Sub ShowSqlException(ByVal connectionString As String)
Dim queryString As String = "EXECUTE NonExistantStoredProcedure"
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
Try
command.Connection.Open()
command.ExecuteNonQuery()
Catch ex As SqlException
DisplaySqlErrors(ex)
End Try
End Using
End Sub
Private Sub DisplaySqlErrors(ByVal exception As SqlException)
Dim i As Integer
For i = 0 To exception.Errors.Count - 1
Console.WriteLine("Index #" & i & ControlChars.NewLine & _
"Error: " & exception.Errors(i).ToString() & ControlChars.NewLine)
Next i
Console.ReadLine()
End Sub
適用於
另請參閱
- ADO.NET 概觀 \(部分機器翻譯\)