当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > ASP.NET - Database Connection 数据库连接

ASP.NET
ASP.NET 介绍
ASP 和 ASP.NET 之间的区别
asp.net的安装
asp.net的网页
ASP.NET - 服务器控件
ASP.NET - 事件
ASP.NET Web 表单
ASP.NET 维持浏览状态
ASP.NET - TextBox(文本框)控件
ASP.NET - 按钮控件
ASP.NET - Data Binding(数据绑定)
ASP.NET - The ArrayList Object(数组列表对象)
ASP.NET - The Hashtable Object 哈希表对象
ASP.NET - The SortedList Object 可排序列表对象
ASP.NET - XML 文件
ASP.NET - Repeater Control 转发器控件
ASP.NET - The DataList Control 数据列表控件
ASP.NET - Database Connection 数据库连接
ASP.NET 2.0 新特征
ASP.NET 2.0 - Master Pages 控制页[主页]

ASP.NET - Database Connection 数据库连接


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1052 ::
收藏到网摘: n/a

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()

评论 (1) 1 All

登陆 | 还没注册?