SqlException.ToString 方法

定义

返回表示当前 SqlException 对象的字符串,并包括客户端连接 ID(有关更多信息,请参见 ClientConnectionId)。

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

返回

一个字符串,表示当前的 SqlException object.String

示例

以下 C# 示例演示尝试连接到有效服务器但不存在的数据库如何导致 SqlException,其中包括客户端连接 ID:

using Microsoft.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 Microsoft.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  

适用于