ASP.NET Web Forms - Database Connection
ADO.NET is also a part of the .NET Framework. ADO.NET is used to handle data access. Through ADO.NET, you can manipulate databases.
Try it - Example
Database Connection - Binding to the DataList Control
Database Connection - Binding to the Repeater Control
What is ADO.NET?
- ADO.NET is a part of the .NET Framework
- ADO.NET consists of a series of classes for handling data access
- ADO.NET is entirely based on XML
- ADO.NET has no 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 database connection in the Page_Load subroutine. We create a dbconn variable and assign it a new OleDbConnection class with a connection string that indicates the OLE DB provider and the database location. Then we open the database connection:
<%@ Import Namespace="System.Data.OleDb" %>
**Note:** This connection string must be a single continuous string with no 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 tables:
<%@ Import Namespace="System.Data.OleDb" %>
Creating a DataReader
The OleDbDataReader class is used to read a stream of records from the data source. The DataReader is created by calling the ExecuteReader method of the OleDbCommand object:
<%@ Import Namespace="System.Data.OleDb" %>
Binding to the Repeater Control
Then, we bind the DataReader to the Repeater control:
Example
<%@ Import Namespace="System.Data.OleDb" %>