SqlException.ToString 方法

定義

傳回字串,代表目前的SqlException物件,並包含用戶端連線識別碼 (如需詳細資訊,請參閱ClientConnectionId)。Returns a string that represents the current SqlException object, and includes the client connection ID (for more information, see ClientConnectionId).

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

表示目前 SqlException 物件的字串StringA string that represents the current SqlException object.String.

範例

下列C#範例顯示連線嘗試連接至有效的伺服器,但不存在的資料庫會造成 SqlException,其中包括用戶端連接識別碼:The following C# example shows how a connection attempt to a valid server but non-existent database causes a SqlException, which includes the client connection ID:

using System.Data.SqlClient;  
using System;  
  
public class A {  
   public static void Main() {  
      SqlConnection connection = new SqlConnection();  
      connection.ConnectionString = "Data Source=a_valid_server;Initial Catalog=Northwinda;Integrated Security=true";  
      try {  
         connection.Open();  
      }  
      catch (SqlException p) {  
         Console.WriteLine("{0}", p.ClientConnectionId);  
         Console.WriteLine("{0}", p.ToString());  
      }  
      connection.Close();  
   }  
}  

下列 Visual Basic 範例在功能上相當於 previous (C#)範例:The following Visual Basic sample is functionally equivalent to the previous (C#) sample:

Imports System.Data.SqlClient  
Imports System  
  
Module Module1  
  
    Sub Main()  
        Dim connection As New SqlConnection()  
        connection.ConnectionString = "Data Source=a_valid_server;Initial Catalog=Northwinda;Integrated Security=true"  
        Try  
            connection.Open()  
        Catch p As SqlException  
            Console.WriteLine("{0}", p.ClientConnectionId)  
            Console.WriteLine("{0}", p.ToString())  
        End Try  
        connection.Close()  
    End Sub  
  
End Module  

適用於