📄 aspnet_dbconnection.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP.NET Database Connection</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP.NET - Database Connection</h1>
<a href="aspnet_datalist.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_newfeatures.asp"><img alt="next" border="0" src="../images/btn_next.gif" /></a>
<hr />
<p class="intro">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.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="showasp.asp@filename=demo_dbconn_repeater">Database connection - Bind to a Repeater control</a></p>
<p><a target="_blank" href="showasp.asp@filename=demo_dbconn_datalist">Database connection - Bind to a DataList control</a></p>
<hr />
<h2>What is ADO.NET?</h2>
<ul>
<li>ADO.NET is a part of the .NET Framework</li>
<li>ADO.NET consists of a set of classes used to handle data access</li>
<li>ADO.NET is entirely based on
XML</li>
<li>ADO.NET has, unlike ADO, no Recordset object</li>
</ul>
<hr />
<h2>Create a Database Connection</h2>
<p>We are going to use the Northwind database in our examples.</p>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data.OleDb" %></pre>
<pre><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></pre>
</td></tr>
</table>
<p> <b>Note:</b> The connection string must be a continuous string without a
line break!</p>
<hr />
<h2>Create a Database Command</h2>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data.OleDb" %></pre>
<pre><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></pre>
</td></tr>
</table>
<br />
<hr />
<h2>Create a DataReader</h2>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data.OleDb" %></pre>
<pre><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></pre>
</td></tr>
</table>
<br />
<hr />
<h2>Bind to a Repeater Control</h2>
<p>Then we bind the DataReader to
a Repeater control:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data.OleDb" %></pre>
<pre><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></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:Repeater id="customers" runat="server"></pre>
<pre><HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Companyname</th>
<th>Contactname</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate></pre>
<pre><ItemTemplate>
<tr>
<td><%#Container.DataItem("companyname")%></td>
<td><%#Container.DataItem("contactname")%></td>
<td><%#Container.DataItem("address")%></td>
<td><%#Container.DataItem("city")%></td>
</tr>
</ItemTemplate></pre>
<pre><FooterTemplate>
</table>
</FooterTemplate></pre>
<pre></asp:Repeater>
</form></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<br />
<hr />
<h2>Close the Database Connection</h2>
<p>Always close both the DataReader and database connection after access to the database is no longer required:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre>dbread.Close()
dbconn.Close()</pre>
</td></tr>
</table>
<br />
<hr />
<a href="aspnet_datalist.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_newfeatures.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -