❑
how to execute SQL queries and retrieve their results using ADO.NET
❑
how to display data that is read from a database
❑
how to handle data access errors
Introducing ADO.NET
In previous chapters, we learned how to use Visual Web Developer and SQL
Management Studio to connect to a database and execute SQL queries. Now,
it’s time to apply this knowledge. Within our web application, we’ll use
ADO.NET’s classes to connect to the database; we’ll then use that connection
to execute SQL queries.
ADO.NET 2.0 and Generic Data Access
ADO.NET is able to use different types of data connections, depending on
the kind of database to which the application is trying to connect. The
ADO.NET classes whose names start with Sql (such as the previously
mentioned SqlConnection, SqlCommand, etc.) are specifically built to
connect to SQL Server.
Similar classes are provided for other databases—for example, if you’re
working with Oracle, you can use classes such as OracleConnection,
OracleCommand, and so on. If, on the other hand, you’re working with
database systems for which such classes are not specifically designed, you
can use generic low-level interfaces; most databases can be accessed through
the OLE DB interface (using classes such as OleDbConnection and
OleDbCommand), or the older ODBC interface (using classes such as
OdbcConnection and OdbcCommand).
In this book, we’ll use only the Sql classes, but it’s good to know that you
have options!
In order to use ADO.NET, we must first decide which kind of database we’ll use,
and import those namespaces containing classes that work with the database.
Since we’re using SQL Server, you’ll need to import the System.Data.SqlClient
namespace. This contains all the required Sql classes, the most important of
which are:
SqlConnection
This class exposes properties and methods for connecting to an SQL Server
database.
332
Chapter 9: ADO.NET