共用方式為


SqlException.ToString 方法

定義

傳回字串,代表目前的SqlException物件,並包含用戶端連線識別碼 (如需詳細資訊,請參閱ClientConnectionId)。

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

傳回

表示目前 SqlException 物件的字串String

範例

下列 C# 範例示範如何嘗試連線至有效的伺服器,但不存在的資料庫會導致 , SqlException其中包含用戶端連線標識碼:

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 範例的功能相當於先前的 (C#) 範例:

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  

適用於