ASP. NET-database connection

Source: Internet
Author: User

ADO. NET is also an integral part of the. NET Framework.

The function of ADO. NET is to process data access. With ADO. net, you can operate the database.

What is ADO. Net?
  • ADO. NET is an integral part of the. NET Framework.
  • ADO. NET is composed of a series of classes used to operate data access.
  • ADO. NET is based entirely on XML
  • Ado. Net does not have a recordset object, which is different from ADO.

Create a database connection
We plan to use the used northwind database. First, import the namespace "system. Data. oledb ". This namespace is required to operate Microsoft Access and other ole db database providers. We will create a connection to this database in the page_load subroutine. We created a dbconn variable and assigned the new oledbconnection class to this variable. This oledbconnection class has a connection string that can indicate the location of ole db 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 a continuous string without any lines!

Create database commands

To specify the records to be retrieved from the database, we will create a dbcomm variable and assign a new oledbcommand to it. This oledbcommand class is used to issue SQL queries for 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 MERs"
dbcomm=New OleDbCommand(sql,dbconn)
End sub
</SCRIPT>

Create datareader

Oledbdatareader class is used to read record streams from data sources. You can create a datareader 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 MERs"
Dbcomm = new oledbcommand (SQL, dbconn)
dbread=dbcomm.ExecuteReader()
End sub
</SCRIPT>

Bind to Repeater control

Then, we bind the 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("/db/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 bgcolor = "# b0c4de">
<TH> companyName </Th>
<TH> contactname </Th>
<TH> address </Th>
<TH> city </Th>
</Tr>
</Headertemplate>

<Itemtemplate>
<Tr bgcolor = "# f0f0f0">
<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>
 
Close database connection

After you do not need to access the database, remember to always close the connection between datareader and the database:

Dbread. Close ()
Dbconn. Close ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.