SqlError Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Collects information relevant to a warning or error returned by SQL Server.
public ref class SqlError sealed
[System.Serializable]
public sealed class SqlError
public sealed class SqlError
[<System.Serializable>]
type SqlError = class
type SqlError = class
Public NotInheritable Class SqlError
- Inheritance
-
SqlError
- Attributes
Examples
The following example displays each SqlError within the SqlErrorCollection collection.
using Microsoft.Data.SqlClient;
using System.Text;
class Program
{
static void Main()
{
string s = GetConnectionString();
ShowSqlException(s);
Console.ReadLine();
}
public static void ShowSqlException(string connectionString)
{
string queryString = "EXECUTE NonExistantStoredProcedure";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
DisplaySqlErrors(ex);
}
}
}
private static void DisplaySqlErrors(SqlException exception)
{
for (int i = 0; i < exception.Errors.Count; i++)
{
Console.WriteLine("Index #" + i + "\n" +
"Error: " + exception.Errors[i].ToString() + "\n");
}
Console.ReadLine();
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=AdventureWorks;"
+ "Integrated Security=SSPI";
}
}
Remarks
This class is created by the .NET Framework Data Provider for SQL Server when an error occurs. An instance of SqlError is created and managed by the SqlErrorCollection, which in turn is created by the SqlException class.
Messages with 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 usually 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 more information on errors generated by SQL Server, see Cause and Resolution of Database Engine Errors. For more information about severity levels, see Database Engine Error Severities.
Properties
Class |
Gets the severity level of the error returned from SQL Server. |
LineNumber |
Gets the line number within the Transact-SQL command batch or stored procedure that contains the error. |
Message |
Gets the text describing the error. |
Number |
Gets a number that identifies the type of error. |
Procedure |
Gets the name of the stored procedure or remote procedure call (RPC) that generated the error. |
Server |
Gets the name of the instance of SQL Server that generated the error. |
Source |
Gets the name of the provider that generated the error. |
State |
Some error messages can be raised at multiple points in the code for the Database Engine. For example, an 1105 error can be raised for several different conditions. Each specific condition that raises an error assigns a unique state code. |
Methods
ToString() |
Gets the complete text of the error message. |