SqlException.Number 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得識別錯誤類型的值。
public:
property int Number { int get(); };
public int Number { get; }
member this.Number : int
Public ReadOnly Property Number As Integer
屬性值
識別錯誤類型的值。
範例
下列範例會顯示集合內的每一個SqlErrorSqlErrorCollection。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
class Program
{
static void Main()
{
string s = GetConnectionString();
ShowSqlException(s);
Console.ReadLine();
}
public static void ShowSqlException(string connectionString)
{
string queryString = "EXECUTE NonExistantStoredProcedure";
StringBuilder errorMessages = new StringBuilder();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"Error Number: " + ex.Errors[i].Number + "\n" +
"LineNumber: " + ex.Errors[i].LineNumber + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Procedure: " + ex.Errors[i].Procedure + "\n");
}
Console.WriteLine(errorMessages.ToString());
}
}
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=AdventureWorks;"
+ "Integrated Security=SSPI";
}
}
Public Sub ShowSqlException(ByVal connectionString As String)
Dim queryString As String = "EXECUTE NonExistantStoredProcedure"
Dim errorMessages As New StringBuilder()
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
Try
command.Connection.Open()
command.ExecuteNonQuery()
Catch ex As SqlException
Dim i As Integer
For i = 0 To ex.Errors.Count - 1
errorMessages.Append("Index #" & i.ToString() & ControlChars.NewLine _
& "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
& "Error Number: " & ex.Errors(i).Number & ControlChars.NewLine _
& "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
& "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
& "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
Next i
Console.WriteLine(errorMessages.ToString())
End Try
End Using
End Sub
備註
這是屬性中Errors第一個 SqlError 屬性的包裝Number函式。 如需 SQL Server 引擎錯誤的詳細資訊,請參閱 Database Engine 事件和錯誤。
適用於
另請參閱
- State
- Class
- Source
- Server
- Procedure
- LineNumber
- ADO.NET 概觀 \(部分機器翻譯\)