Catching SQL Errors
Two distinct exception classes exist for catching exceptions, if they are thrown. Depending on which data class you are working with, you will obviously need to use the appropriate exception class. For the examples in this section, we will be using the SqlClient class, so we will use the SqlException class to catch any exception thrown. If you are using the OleDb class, you will need to use the corresponding OleDbException class to catch exceptions.
The SqlException class is designed to handle exceptions that are thrown while executing a SQL statement. When an exception is thrown, you can gather enough information in the SqlException class to figure out what is happening. We have written a small function (see Listings 16.1 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access