ASP.NET Database Connection
-- Learning is not just about technology, but also about dreams!
Home HTML JAVASCRIPT CSS VUE REACT PYTHON3 JAVA C C++ C# AI GO SQL LINUX VS CODE BOOTSTRAP GIT Local Bookmarks
ASP.NET Tutorial
WP Tutorial
- WebPages Introduction
- WebPages Razor
- WebPages Layout
- WebPages Folders
- WebPages Global
- WebPages Forms
- WebPages Objects
- WebPages Files
- WebPages Helpers
- WebPages WebGrid
- WebPages Charts
- WebPages Email
- WebPages PHP
- WebPages Publishing
- WebPages Examples
WP Reference Manual
ASP.NET Razor
- Razor Introduction
- Razor Syntax
- Razor C# Variables
- Razor C# Loops
- Razor C# Logic
- Razor VB Variables
- Razor VB Loops
- Razor VB Logic
ASP.NET MVC
- MVC Introduction
- MVC Applications
- MVC Folders
- MVC Layout
- MVC Controllers
- MVC Views
- MVC Database
- MVC Models
- MVC Security
- MVC HTML Helpers
- MVC Publishing
- MVC Reference Manual
WF Tutorial
- WebForms Introduction
- WebForms Pages
- WebForms Controls
- WebForms Events
- WebForms Forms
- WebForms ViewState
- WebForms TextBox
- WebForms Button
- WebForms Data Binding
- WebForms ArrayList
- WebForms Hashtable
- WebForms SortedList
- WebForms XML Files
- WebForms Repeater
- WebForms DataList
- WebForms Database Connection
- WebForms Master Pages
- WebForms Navigation
- WebForms Examples
WF Reference Manual
ASP.NET DataList Control
ASP.NET Master Pages
Deep Dive
- Computer Science
- Programming
- Software
- Scripting Languages
- Web Service
- Programming Languages
- Scripts
- Development Tools
- Web Design & Development
- Web Services
ASP.NET Web Forms - Database Connection
ADO.NET is also a part of the .NET framework. ADO.NET is used for handling data access. With ADO.NET, you can manipulate databases.
Try It - Examples
Database Connection - Binding to a DataList Control
Database Connection - Binding to a 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 fully based on XML
- ADO.NET does not have a Recordset object, which is different from 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 variable named dbconn and assign it a new OleDbConnection class with a connection string indicating the OLE DB provider and the database location. Then we open the database connection:
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
end sub
</script>
Note: This connection string must be a continuous string without line breaks!
Creating a Database Command
To specify which records to retrieve from the database, we will create a variable named dbcomm and assign it a new OleDbCommand class. This OleDbCommand class is used to issue a SQL query against a database table:
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
end sub
</script>
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:
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
end sub
</script>
Binding to a Repeater Control
Then, we bind the DataReader to a Repeater control:
Example
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="customers" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Companyname</th>
<th>Contactname</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("companyname")%></td>
<td><%#Container.DataItem("contactname")%></td>
<td><%#Container.DataItem("address")%></td>
<td><%#Container.DataItem("city")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
Closing the Database Connection
Remember to close the DataReader and the database connection when you no longer need access to the database:
dbread.Close()
dbconn.Close()
ASP.NET DataList Control
ASP.NET Master Pages
Byte Ark Coding Plan
Supports mainstream large models like Doubao, GLM, DeepSeek, Kimi, MiniMax, etc. Officially supplied, stable and reliable.
¥9.9 / month | Subscribe Now
iFlytek Starlight Coding Plan
Includes free model call quotas for DeepSeek, GLM, Kimi, MiniMax, a one-stop experience and deployment platform.
¥3.9 / month | Subscribe Now
Category Navigation
- Python / Data Science
- AI / Intelligent Development
- Front-end Development
- Back-end Development
- Databases
- Mobile Development
- DevOps / Engineering
- Programming Languages
- Computer Fundamentals
- XML / Web Service
- .NET
- Website Construction
Advertisement
ASP.NET Tutorial
WP Tutorial
- WebPages Introduction
- WebPages Razor
- WebPages Layout
- WebPages Folders
- WebPages Global
- WebPages Forms
- WebPages Objects
- WebPages Files
- WebPages Helpers
- WebPages WebGrid
- WebPages Charts
- WebPages Email
- WebPages PHP
- WebPages Publishing
- WebPages Examples
WP Reference Manual
ASP.NET Razor
- Razor Introduction
- Razor Syntax
- Razor C# Variables
- Razor C# Loops
- Razor C# Logic
- Razor VB Variables
- Razor VB Loops
- Razor VB Logic
ASP.NET MVC
- MVC Introduction
- MVC Applications
- MVC Folders
- MVC Layout
- MVC Controllers
- MVC Views
- MVC Database
- MVC Models
- MVC Security
- MVC HTML Helpers
- MVC Publishing
- MVC Reference Manual
WF Tutorial
- WebForms Introduction
- WebForms Pages
- WebForms Controls
- WebForms Events
- WebForms Forms
- WebForms ViewState
- WebForms TextBox
- WebForms Button
- WebForms Data Binding
- WebForms ArrayList
- WebForms Hashtable
- WebForms SortedList
- WebForms XML Files
- WebForms Repeater
- WebForms DataList
- WebForms Database Connection
- WebForms Master Pages
- WebForms Navigation
- WebForms Examples
WF Reference Manual
Online Examples
- · HTML Examples
- · CSS Examples
- · JavaScript Examples
- · Ajax Examples
- · jQuery Examples
- · XML Examples
- · Java Examples
Character Sets & Tools
- · HTML Character Set Settings
- · HTML ASCII Character Set
- · JS Obfuscation/Encryption
- · PNG/JPEG Image Compression
- · HTML Color Picker
- · JSON Formatting Tool
- · Random Number Generator
Latest Updates
- · AI Agents
- · AI Evaluation & Safety Research
- · AI System Architecture
- · Cutting-edge Research Trends
- · Advanced NLP Techniques
- · Computer Vision AI
- · Deep Learning Fundamentals
Site Information
Follow WeChat
My Favorites
Mark Article
Browsing History
Clear All
No records
YouTip