ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - Database Connection Êý¾Ý¿âÁ¬½Ó

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

ASP.NET
asp.netµÄ°²×°
ASP ºÍ ASP.NET Ö..
ASP.NET ½éÉÜ

ASP.NET - Database Connection Êý¾Ý¿âÁ¬½Ó


×÷Õß:w3schools ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-09 ä¯ÀÀ:2810 :: ::

ADO.NET is also a part of the .NET Framework. ADO.NET is used to handle data access. With ADO.NET you can work with databases.
ADO.NET Ò²ÊÇ.NET Framework µÄÒ»²¿·Ö¡£ADO.NET ¿ÉÓÃÀ´´¦ÀíÊý¾Ý·ÃÎÊ¡£Ê¹ÓÃADO.NETÄã¿ÉÒÔ²Ù×÷Êý¾Ý¿â¡£


Examples ¾ÙÀý

Database connection - Bind to a Repeater control

Database connection - Bind to a DataList control


What is ADO.NET?
ʲôÊÇ ADO.NET?

  • ADO.NET is a part of the .NET Framework
    ADO.NET ÊÇ .NET Framework µÄÒ»²¿·Ö
  • ADO.NET consists of a set of classes used to handle data access
    ADO.NET °üÀ¨Ò»×é¿ÉÓÃÀ´´¦ÀíÊý¾Ý·ÃÎʵÄÀà¿â
  • ADO.NET is entirely based on XML
    ADO.NET ÍêÈ«»ùÓÚXML
  • ADO.NET has, unlike ADO, no Recordset object
    ADO.NET ²»Ïñ ADO£¬ËüûÓÐÊý¾Ý¼¯¶ÔÏó

Create a Database Connection
½¨Á¢Ò»ÌõÊý¾Ý¿âÁ¬½Ó

We are going to use the Northwind database in our examples.
½ÓÏÂÀ´ÎÒÃǽ«ÒªÔÚ¾ÙÀýÖÐʹÓÃNorthwind Êý¾Ý¿â

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 the database in the Page_Load subroutine. We create a dbconn variable as a new OleDbConnection class with a connection string which identifies the OLE DB provider and the location of the database. Then we open the database connection:
Ê×ÏÈ£¬µ¼Èë "System.Data.OleDb" ÃüÃû¿Õ¼ä£¬ÎÒÃÇÐèÒªÕâ¸öÃüÃû¿Õ¼ä¸ú Microsoft Access ÒÔ¼°ÆäËûµÄOLE DB Êý¾Ý¿âÌṩÉÌÒ»ÆðÔË×÷¡£ÎÒÃǽ«ÔÚPage_Load ×Ó³ÌÐòÖн¨Á¢µ½Êý¾Ý¿âµÄÁ¬½Ó¡£ÎÒÃǽ¨Á¢µÄ±äÁ¿ dbconn À´×÷ΪһÌõÐ嵀 OleDbConnection À࣬ÕâÐèҪͨ¹ýÒ»´®Äܹ»ÈÃOLE DBÌṩÉÌʶ±ðµÄ¶ö×Ö·û´®²¢ÇÒÓÐÓÚÖ®¶ÔÓ¦µÄ±¾µØÊý¾Ý¿â¡£È»ºóÎÒÃǾʹò¿ªÁËÊý¾Ý¿âÁ¬½Ó£º

<%@ 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 a line break!
×¢Ò⣺Á¬½Ó×Ö·û´®±ØÐëÊÇÁ¬ÐøµÄ£¬Öм䲻ÄÜ»»ÐУ¡


Create a Database Command
½¨Á¢Êý¾Ý¿âÃüÁî

To specify the records to retrieve from the database, we will create a dbcomm variable as a new OleDbCommand class. The OleDbCommand class is for issuing SQL queries against database tables:
Òª´ÓÊý¾Ý¿âÈ¡»ØÖ¸¶¨µÄ¼Ç¼£¬ÎÒÃǽ«Òª½¨Á¢Ò»¸ö±äÁ¿ dbcomm À´×÷ΪOleDbCommand Àà¡£OleDbCommandÊÇ¿ÉÕë¶ÔÊý¾Ý±íÀ´·¢ËÍSQL²éѯµÄÀࣺ

<%@ 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 a DataReader
½¨Á¢Ò»¸öÊý¾Ý¶ÁÈ¡Æ÷

The OleDbDataReader class is used to read a stream of records from a data source. A DataReader is created by calling the ExecuteReader method of the OleDbCommand object:
OleDbDataReader Àà¿ÉÓÃÀ´¶ÁÈ¡À´×ÔÊý¾ÝÔ´µÄ¼Ç¼Á÷¡£Í¨¹ýµ÷ÓÃOleDbCommand ¶ÔÏóÖÐµÄ ExecuteReader·½·¨¾Í¿ÉÒÔ½¨Á¢Ò»¸öÊý¾Ý¶ÁÈ¡Æ÷£º

<%@ 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>

 


Bind to a 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("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>

 


Close the Database Connection
¹Ø±ÕÊý¾Ý¿âÁ¬½Ó

Always close both the DataReader and database connection after access to the database is no longer required:
ÔÚ²»ÐèÒªÊý¾Ý¿â·ÃÎʵÄʱºò°ÑÊý¾Ý¶ÁÈ¡Æ÷ÒÔ¼°Êý¾Ý¿âÁ¬½Ó¶¼¹Ø±Õµô£º

dbread.Close()
dbconn.Close()

ÆÀÂÛ (0) All