Analysis on ASP. NET database connection

Source: Internet
Author: User

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:

 
 
  1. ﹤%@ Import Namespace="System.Data.OleDb" %﹥  
  2. ﹤script runat="server"﹥  
  3. sub Page_Load  
  4. dim dbconn  
  5. dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  6. data source=" & server.mappath("northwind.mdb"))  
  7. dbconn.Open()  
  8. end sub  
  9. ﹤/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:

 
 
  1. ﹤%@ Import Namespace="System.Data.OleDb" %﹥  
  2. ﹤script runat="server"﹥  
  3. sub Page_Load  
  4. dim dbconn,sql,dbcomm  
  5. dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  6. data source=" & server.mappath("northwind.mdb"))  
  7. dbconn.Open()  
  8. sql="SELECT * FROM customers" 
  9. dbcomm=New OleDbCommand(sql,dbconn)  
  10. end sub  
  11. ﹤/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:

 
 
  1. ﹤%@ Import Namespace="System.Data.OleDb" %﹥  
  2. ﹤script runat="server"﹥  
  3. sub Page_Load  
  4. dim dbconn,sql,dbcomm,dbread  
  5. dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  6. data source=" & server.mappath("northwind.mdb"))  
  7. dbconn.Open()  
  8. sql="SELECT * FROM customers" 
  9. dbcomm=New OleDbCommand(sql,dbconn)  
  10. dbread=dbcomm.ExecuteReader()  
  11. end sub  
  12. ﹤/script﹥ 

Binding ASP. NET database connection to Repeater control

Then we bind DataReader to a Repeater control:

 
 
  1. ﹤%@ Import Namespace="System.Data.OleDb" %﹥  
  2. ﹤script runat="server"﹥  
  3. sub Page_Load  
  4. dim dbconn,sql,dbcomm,dbread  
  5. dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  6. data source=" & server.mappath("northwind.mdb"))  
  7. dbconn.Open()  
  8. sql="SELECT * FROM customers" 
  9. dbcomm=New OleDbCommand(sql,dbconn)  
  10. dbread=dbcomm.ExecuteReader()  
  11. customers.DataSource=dbread  
  12. customers.DataBind()  
  13. dbread.Close()  
  14. dbconn.Close()  
  15. end sub  
  16. ﹤/script﹥  
  17. ﹤html﹥  
  18. ﹤body﹥  
  19. ﹤form runat="server"﹥  
  20. ﹤asp:Repeater id="customers" runat="server"﹥  
  21. ﹤HeaderTemplate﹥  
  22. ﹤table border="1" width="100%"﹥  
  23. ﹤tr﹥  
  24. ﹤th﹥Companyname﹤/th﹥  
  25. ﹤th﹥Contactname﹤/th﹥  
  26. ﹤th﹥Address﹤/th﹥  
  27. ﹤th﹥City﹤/th﹥  
  28. ﹤/tr﹥  
  29. ﹤/HeaderTemplate﹥  
  30. ﹤ItemTemplate﹥  
  31. ﹤tr﹥  
  32. ﹤td﹥﹤%#Container.DataItem("companyname")%﹥﹤/td﹥  
  33. ﹤td﹥﹤%#Container.DataItem("contactname")%﹥﹤/td﹥  
  34. ﹤td﹥﹤%#Container.DataItem("address")%﹥﹤/td﹥  
  35. ﹤td﹥﹤%#Container.DataItem("city")%﹥﹤/td﹥  
  36. ﹤/tr﹥  
  37. ﹤/ItemTemplate﹥  
  38. ﹤FooterTemplate﹥  
  39. ﹤/table﹥  
  40. ﹤/FooterTemplate﹥  
  41. ﹤/asp:Repeater﹥  
  42. ﹤/form﹥  
  43. ﹤/body﹥  
  44. ﹤/html﹥ 

ASP. NET database connection close database connection

After accessing the database, you always disable unnecessary DataReader and database connections:

 
 
  1. dbread.Close()  
  2. dbconn.Close() 

ASP. NET database connection will introduce you here, I hope you have some understanding of ASP. NET database connection.

  1. Analysis on the AutoComplete control of Asp.net Ajax Control
  2. Analysis on installing ASP. NET on Windows Server 2003
  3. Analysis on ASP. NET runtime environment Establishment
  4. ASP. NET Overview
  5. Analysis on the advantages of ASP. NET in eleven aspects

Related Article

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.