SqlError.ToString 方法
定义
获取错误信息的完整文本。Gets the complete text of the error message.
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
返回
错误信息的完整文本。The complete text of the error.
示例
下面的示例显示 SqlError 集合中的每个 SqlErrorCollection 。The following example displays each SqlError within the SqlErrorCollection collection.
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
注解
字符串的格式为 "SqlError:",后跟 Message 和堆栈跟踪。The string is in the form "SqlError:", followed by the Message, and the stack trace. 例如:For example:
SqlError: UserId 或 Password 无效。SqlError:UserId or Password not valid. <stack trace>