YouTip LogoYouTip

Aspnet Dbconnection

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

WP Reference Manual

ASP.NET Razor

ASP.NET MVC

WF Tutorial

WF Reference Manual

ASP.NET DataList Control

ASP.NET Master Pages

Deep Dive

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>

View Demo Example »

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.

Configuration Guide

¥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.

Configuration Guide

¥3.9 / month | Subscribe Now

Click me to share notes

Category Navigation

Advertisement

ASP.NET Tutorial

WP Tutorial

WP Reference Manual

ASP.NET Razor

ASP.NET MVC

WF Tutorial

WF Reference Manual

Online Examples

Character Sets & Tools

Latest Updates

Site Information

Follow WeChat

My Favorites

Mark Article

Browsing History

Clear All

No records

← Ng Ng ChangeNg Ng Blur →