Before connecting to the ASP. NET database, Let's first look at what is ADO. NET? ADO. NET is part of the. NET Framework and is used to handle data access problems. With ADO. NET, you can work with databases.
ADO. NET is part of the. NET Framework.
ADO. NET is composed of a group of classes that process data access.
ADO. NET is based entirely on XML
Unlike ADO. NET, ADO. NET does not have a Recordset object.
ASP. NET database connection Establishment
We are going to use the Northwind database in our example.
First, import the namespace "System. Data. OleDb ". We need this namespace to work with Microsoft Access and other database providers. We will establish a connection with the database in the Page_Load subroutine. We create a variable dbconn as a new OleDbConnection class, which has a connection string to specify the location of the ole db provider and database. 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: The connection string must be continuous without line breaks!
Create database commands for ASP. NET database connection
To specify the retrieved records from the database, we will create a variable dbcomm as a new OleDbCommand class. The OleDbCommand class is used to issue SQL queries to database tables:
- ﹤%@ 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﹥
Create DataReader for ASP. NET database connection
OleDbDataReader class is used to read record streams from data sources. 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 ASP. NET database connection to Repeater control
Then we bind DataReader to a Repeater control:
- ﹤%@ 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﹥
ASP. NET database connection close database connection
After accessing the database, you always disable unnecessary DataReader and database connections:
- dbread.Close()
- dbconn.Close()
ASP. NET database connection will introduce you here, I hope you have some understanding of ASP. NET database connection.
- Analysis on the AutoComplete control of Asp.net Ajax Control
- Analysis on installing ASP. NET on Windows Server 2003
- Analysis on ASP. NET runtime environment Establishment
- ASP. NET Overview
- Analysis on the advantages of ASP. NET in eleven aspects