SqlException.Errors Property
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.
Gets a collection of one or more SqlError objects that give detailed information about exceptions generated by the .NET Framework Data Provider for SQL Server.
public:
property Microsoft::Data::SqlClient::SqlErrorCollection ^ Errors { Microsoft::Data::SqlClient::SqlErrorCollection ^ get(); };
public Microsoft.Data.SqlClient.SqlErrorCollection Errors { get; }
member this.Errors : Microsoft.Data.SqlClient.SqlErrorCollection
Public ReadOnly Property Errors As SqlErrorCollection
Property Value
The collected instances of the SqlError class.
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
The SqlErrorCollection class always contains at least one instance of the SqlError class.
This is a wrapper for SqlErrorCollection. For more information on SQL Server engine errors, see Database Engine Events and Errors.