How do you handle exceptions in ADO.NET?
DO.NET exceptions are typically handled using try-catch blocks. This helps to capture any
errors during database operations such as connection failures, query issues, or command
execution errors.
Example:
try
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM Customers",
connection);
SqlDataReader reader = command.ExecuteReader();
}
catch (SqlException ex)
{
Console.WriteLine("Database error: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("General error: " + ex.Message);
}