Assembly: System.Data (in system.data.dll)
'Declaration <SerializableAttribute> _ Public NotInheritable Class SqlException Inherits DbException 'Usage Dim instance As SqlException
/** @attribute SerializableAttribute() */ public final class SqlException extends DbException
This class is created whenever the .NET Framework Data Provider for SQL Server encounters an error generated from the server. (Client side errors are thrown as standard common language runtime exceptions.) SqlException always contains at least one instance of SqlError.
Messages that have a severity level of 10 or less are informational and indicate problems caused by mistakes in information that a user has entered. Severity levels from 11 through 16 are generated by the user, and can be corrected by the user. Severity levels from 17 through 25 indicate software or hardware errors. When a level 17, 18, or 19 error occurs, you can continue working, although you might not be able to execute a particular statement.
The SqlConnection remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server ordinarily closes the SqlConnection. However, the user can reopen the connection and continue. In both cases, a SqlException is generated by the method executing the command.
For information about the warning and informational messages sent by SQL Server, see the Troubleshooting section of SQL Server Books Online. The SqlException class maps to SQL Server severity.
The following example generates a SqlException and then displays the exception.
Public Sub ShowSqlException(ByVal connectionString As String) Dim queryString As String = "EXECUTE NonExistantStoredProcedure" Dim errorMessages As New StringBuilder() Using connection As New SqlConnection(connectionString) Dim command As New SqlCommand(queryString, connection) Try command.Connection.Open() command.ExecuteNonQuery() Catch ex As SqlException Dim i As Integer For i = 0 To ex.Errors.Count - 1 errorMessages.Append("Index #" & i.ToString() & ControlChars.NewLine _ & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _ & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _ & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _ & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine) Next i Console.WriteLine(errorMessages.ToString()) End Try End Using End Sub
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.