数据源控件与数据绑定控件交互并隐藏复杂的数据绑定过程。这些工具为数据绑定控件提供数据,并支持插入,删除,排序和更新等操作的执行。
每个数据源控件包装一个特定的数据提供程序 - 关系数据库,xml文档或自定义类,并帮助我们:
在asp.net中有许多数据源控件可用于从sql server,odbc或ole db服务器,xml文件和业务对象访问数据。
根据数据的类型,这些控件可以分为两大类:
用于分层数据的数据源控件是:
用于表格数据的数据源控件是:
编号 | 数据源控件 | 描述 |
---|---|---|
1 | sqldatasource |
它表示与返回sql数据的ado.net数据提供者的连接,包括可通过oledb 和odbc 访问的数据源。 |
2 | objectdatasource |
它允许绑定到返回数据的自定义.net业务对象。 |
3 | linqdatasource |
它允许绑定到linq-to-sql 查询的结果(仅由asp.net 3.5支持)。 |
4 | accessdatasource |
它表示与microsoft access 数据库的连接。 |
数据源视图是datasourceview
类的对象。它代表不同数据操作(如排序,过滤等)的数据定制视图。
datasourceview
类用作所有数据源视图类的基类,这些类定义了数据源控件的功能。
下表提供了datasourceview
类的属性:
编号 | 属性 | 描述 |
---|---|---|
1 | candelete |
指示是否允许在基础数据源上进行删除。 |
2 | caninsert |
指示是否允许在基础数据源上插入。 |
3 | canpage |
指示是否允许在基础数据源上进行分页。 |
4 | canretrievetotalrowcount |
指示总行数信息是否可用。 |
5 | cansort |
指示是否可以对数据进行排序。 |
6 | canupdate |
指示是否允许在基础数据源上进行更新。 |
7 | events |
获取数据源视图的事件处理程序委托的列表。 |
8 | name |
视图的名称。 |
下表提供了datasourceview
类的方法:
编号 | 方法 | 描述 |
---|---|---|
1 | canexecute |
确定是否可以执行指定的命令。 |
2 | executecommand |
执行特定的命令。 |
3 | executedelete |
对datasourceview 对象所表示的数据列表执行删除操作。 |
4 | executeinsert |
对datasourceview 对象表示的数据列表执行插入操作。 |
5 | executeselect |
获取底层数据存储的数据列表。 |
6 | executeupdate |
对datasourceview 对象表示的数据列表执行更新操作。 |
7 | delete |
对与视图关联的数据执行删除操作。 |
8 | insert |
对与视图关联的数据执行插入操作。 |
9 | select |
返回查询的数据。 |
10 | update |
对与视图关联的数据执行更新操作。 |
11 | ondatasourceviewchanged |
引发datasourceviewchanged 事件。 |
12 | raiseunsupportedcapabilitieserror |
由raiseunsupportedcapabilitieserror 方法调用,以将executeselect 操作请求的功能与视图支持的功能进行比较。 |
sqldatasource
控件表示与关系数据库(如sql server或oracle数据库)的连接,或可通过oledb
或开放式数据库连接(odbc)访问的数据。连接数据是通过两个重要的属性connectionstring
和providername
来完成的。
以下代码片段提供了该控件的基本语法:
<asp:sqldatasource runat="server" id="mysqlsource"
providername='<%$ connectionstrings:localnwind.providername %>'
connectionstring='<%$ connectionstrings:localnwind %>'
selectioncommand= "select * from employees" />
<asp:gridview id="gridview1" runat="server" datasourceid="mysqlsource" />
在底层数据上配置各种数据操作取决于数据源控件的各种属性(属性组)。
下表提供了sqldatasource
控件的相关属性集,它提供了控件的编程接口:
编号 | 属性集/组 | |
---|---|---|
1 | deletecommand ,deleteparameters ,deletecommandtype |
获取或设置用于删除基础数据中的行的sql语句,参数和类型。 |
2 | filterexpression ,filterparameters |
获取或设置数据过滤字符串和参数。 |
3 | insertcommand ,insertparameters ,insertcommandtype |
获取或设置用于在基础数据库中插入行的sql语句,参数和类型。 |
4 | selectcommand ,selectparameters ,selectcommandtype |
获取或设置从底层数据库检索行的sql语句,参数和类型。 |
5 | sortparametername |
获取或设置命令的存储过程将用于排序数据的输入参数的名称。 |
5 | updatecommand ,updateparameters ,updatecommandtype |
获取或设置用于更新基础数据存储中的行的sql语句,参数和类型。 |
以下代码片段显示了为数据操作启用的数据源控件:
<asp:sqldatasource runat="server" id= "mysqlsource"
providername='<%$ connectionstrings:localnwind.providername %>'
connectionstring=' <%$ connectionstrings:localnwind %>'
selectcommand= "select * from employees"
updatecommand= "update employees set lastname=@lame"
deletecommand= "delete from employees where employeeid=@eid"
filterexpression= "employeeid > 10">
.....
.....
</asp:sqldatasource>
objectdatasource
控件允许用户定义的类将其方法的输出关联到数据绑定控件。该类的编程接口与sqldatasource
控件几乎相同。
以下是绑定业务对象的两个重要方面:
让我们直接看一个例子来处理这个控件。 student
类是与对象数据源一起使用的类。 这个类有三个属性:学生id,姓名和城市。 它有一个默认的构造函数和一个getstudents
方法来检索数据。
student
类代码如下:
using system;
using system.collections.generic;
using system.data;
using system.linq;
using system.web;
/// <summary>
/// student 的摘要说明
/// </summary>
public class student
{
public int studentid { get; set; }
public string name { get; set; }
public string city { get; set; }
public student()
{ }
public dataset getstudents()
{
dataset ds = new dataset();
datatable dt = new datatable("students");
dt.columns.add("学生编号", typeof(system.int32));
dt.columns.add("姓名", typeof(system.string));
dt.columns.add("城市", typeof(system.string));
dt.rows.add(new object[] { 1001, "何马", "海口" });
dt.rows.add(new object[] { 1002, "李小静", "上海" });
ds.tables.add(dt);
return ds;
}
}
执行以下步骤将对象与对象数据源绑定并检索数据:
students.cs
),并将上面的代码放入其中。objectdatasource
控件放置在web窗体中。如下图所示 -gridview
),并选择对象数据源作为其基础数据源。文件 default.aspx 中的代码如下所示 -
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>数据源示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:objectdatasource id="objectdatasource1" runat="server" selectmethod="getstudents" typename="student"></asp:objectdatasource>
<br />
<asp:gridview id="gridview1" runat="server" datasourceid="objectdatasource1">
</asp:gridview>
</div>
</form>
</body>
</html>
运行该项目,它从student
类中检索硬编码的元组,并显示其中的数据如下 -
accessdatasource
控件表示到access数据库的连接。它基于sqldatasource
控件,并提供更简单的编程接口。以下代码片段提供了数据源的基本语法:
<asp:accessdatasource id="accessdatasource1 runat="server"
datafile="~/app_data/aspdotnetstepbystep.mdb" selectcommand="select * from [dotnetreferences]">
</asp:accessdatasource>
accessdatasource
控件以只读模式打开数据库。但是,它也可以用于执行插入,更新或删除操作。这是使用ado.net命令和参数集合完成的。
从asp.net应用程序中更新access数据库是有问题的,因为access
数据库是普通文件,asp.net应用程序的默认帐户可能没有写入数据库文件的权限。