⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspnet_dbconnection.asp@output=print

📁 W3Schools tutorial..web designing
💻 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 &quot;System.Data.OleDb&quot; 
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>&lt;%@ Import Namespace=&quot;System.Data.OleDb&quot; %&gt;</pre>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
dim dbconn
dbconn=New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=&quot; &amp; server.mappath(&quot;northwind.mdb&quot;))
dbconn.Open()
end sub
&lt;/script&gt;</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>&lt;%@ Import Namespace=&quot;System.Data.OleDb&quot; %&gt;</pre>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=&quot; &amp; server.mappath(&quot;northwind.mdb&quot;))
dbconn.Open()
sql=&quot;SELECT * FROM customers&quot;
dbcomm=New OleDbCommand(sql,dbconn)
end sub
&lt;/script&gt;</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>&lt;%@ Import Namespace=&quot;System.Data.OleDb&quot; %&gt;</pre>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=&quot; &amp; server.mappath(&quot;northwind.mdb&quot;))
dbconn.Open()
sql=&quot;SELECT * FROM customers&quot;
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
end sub
&lt;/script&gt;</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>&lt;%@ Import Namespace=&quot;System.Data.OleDb&quot; %&gt;</pre>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=&quot; &amp; server.mappath(&quot;northwind.mdb&quot;))
dbconn.Open()
sql=&quot;SELECT * FROM customers&quot;
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
&lt;/script&gt;</pre>
<pre>&lt;html&gt;
&lt;body&gt;</pre>
<pre>&lt;form runat=&quot;server&quot;&gt;
&lt;asp:Repeater id=&quot;customers&quot; runat=&quot;server&quot;&gt;</pre>
<pre>&lt;HeaderTemplate&gt;
&lt;table border=&quot;1&quot; width=&quot;100%&quot;&gt;
&lt;tr&gt;
&lt;th&gt;Companyname&lt;/th&gt;
&lt;th&gt;Contactname&lt;/th&gt;
&lt;th&gt;Address&lt;/th&gt;
&lt;th&gt;City&lt;/th&gt;
&lt;/tr&gt;
&lt;/HeaderTemplate&gt;</pre>
<pre>&lt;ItemTemplate&gt;
&lt;tr&gt;
&lt;td&gt;&lt;%#Container.DataItem(&quot;companyname&quot;)%&gt;&lt;/td&gt;
&lt;td&gt;&lt;%#Container.DataItem(&quot;contactname&quot;)%&gt;&lt;/td&gt;
&lt;td&gt;&lt;%#Container.DataItem(&quot;address&quot;)%&gt;&lt;/td&gt;
&lt;td&gt;&lt;%#Container.DataItem(&quot;city&quot;)%&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/ItemTemplate&gt;</pre>
<pre>&lt;FooterTemplate&gt;
&lt;/table&gt;
&lt;/FooterTemplate&gt;</pre>
<pre>&lt;/asp:Repeater&gt;
&lt;/form&gt;</pre>
<pre>&lt;/body&gt;
&lt;/html&gt;</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 + -