Export (0) Print
Expand All

SqlException.ToString Method

.NET Framework 4.5

Returns a string that represents the current SqlException object, and includes the client connection ID (for more information, see ClientConnectionId).

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Overrides Function ToString As String

Return Value

Type: System.String
A string that represents the current SqlException object.String.

Implements

_Exception.ToString

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();
   }
}

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

.NET Framework

Supported in: 4.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft