ASP.NET Web Forms - Database Connection
ADO.NET is also a part of the .NET framework. ADO.NET is used for data access. Through ADO.NET, you can manipulate databases.
Try It Yourself - Examples
Database Connection - Binding to DataList Control
Database Connection - Binding to Repeater Control
What is ADO.NET?
- ADO.NET is a part of the .NET framework
- ADO.NET consists of a series of classes for data access
- ADO.NET is entirely based on XML
- ADO.NET does not have a Recordset object, unlike ADO
Creating a Database Connection
In our example, we will use the Northwind database.
First, import the "System.Data.OleDb" namespace. We need this namespace to work with Microsoft Access and other OLE DB database providers. We will create the connection to this database in the Page_Load subroutine. We create a dbconn variable and assign it a new OleDbConnection class, which has a connection string indicating the OLE DB provider and the database location. Then we open the database connection:
Note: This connection string must be a continuous string without line breaks!
Creating a Database Command
To specify the records to be retrieved from the database, we will create a dbcomm variable and assign it a new OleDbCommand class. This OleDbCommand class is used to issue SQL queries against the database table:
Creating a DataReader
The OleDbDataReader class is used to read a stream of records from a data source. The DataReader is created by calling the ExecuteReader method of the OleDbCommand object:
Binding to Repeater Control
Then, we bind the DataReader to the Repeater control:
Example
Closing the Database Connection
If you no longer need to access the database, remember to close the DataReader and the database connection: