服务器端数据介绍
数据访问是任何实际应用程序的核心部分,而 asp.net 提供了一套丰富的控件,这些控件与公共语言运行库中提供的托管数据访问 api 很好地集成在一起。 本节多次演练同一个示例,该示例使用 asp.net datagrid 控件绑定到 sql 查询的结果和 xml 数据文件。 本节假定您熟悉数据库基础知识和 sql 查询语言。
服务器端数据访问很独特,因为 web 页基本上是无状态的。当试图执行事务时,如插入或更新从数据库检索的数据集中的记录时,这向我们提出了某些困难的挑战。 正如将在本节中看到的,datagrid 控件可以帮助应付这些挑战,使您得以更多地集中在应用程序逻辑上,对状态管理和事件处理的具体细节则不用考虑太多。
连接、命令和数据集
公共语言运行库为数据密集的应用程序开发提供了完整的托管数据访问 api 集。 这些 api 帮助抽象数据并用一致的方法表示数据,与实际的数据源(sql server、oledb、xml 等)无关。 最常使用的对象基本上有三种:连接、命令和数据集。
连接表示与某些数据存储区(如 sql server 或 xml 文件)的物理连接。
命令表示从数据存储区检索(选择)或对数据存储区进行操作(插入、更新、删除)的指令。
数据集表示应用程序使用的实际数据。 注意,数据集总是同它们的源连接和数据模型断开并可独立修改。 不过,数据集的更改可以很容易与起始数据模型相协调。
有关公共语言运行库中托管数据访问解决方案的更详细演练,请阅读本教程的 ado.net 概述一节。
访问基于 sql 的数据
应用程序一般需要对 sql 数据库执行一个或多个选择、插入、更新或删除查询。 下表显示上述每个查询的示例。
查询
示例
简单选择
select * from employees where firstname = 'bradley';
联接选择
select * from employees e, managers m where e.firstname = m.firstname;
插入
insert into employees values ('123-45-6789','bradley','millington','program manager');
更新
update employees set title = 'development lead' where firstname = 'bradley';
删除
delete from employees where productivity < 10;
为了使页能够访问执行 sql 数据访问所需的类,必须将 system.data 和 system.data.sqlclient 命名空间导入到页中。
<%@ import namespace="system.data" %> <%@ import namespace="system.data.sqlclient" %>
若要对 sql 数据库执行选择查询,请创建与数据库的 sqlconnection,传递连接字符串,然后构造包含查询语句的 sqldataadapter 对象。 若要用查询结果填充 dataset 对象,请调用命令的 fill 方法。
sqlconnection myconnection = new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes"); sqldataadapter mycommand = new sqldataadapter("select * from authors", myconnection); dataset ds = new dataset(); mycommand.fill(ds, "authors"); dim myconnection as new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes") dim mycommand as new sqldataadapter("select * from authors", myconnection) dim ds as new dataset() mycommand.fill(ds, "authors") var myconnection:sqlconnection = new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes"); var mycommand:sqldataadapter = new sqldataadapter("select * from authors", myconnection); var ds:dataset = new dataset(); mycommand.fill(ds, "authors");
正如本节前面所提到的,使用数据集的好处是它为您提供了断开连接的数据库视图。 可以在应用程序中操作数据集,然后在以后协调更改和实际的数据库。 对于长期运行的应用程序,这通常是最好的方法。 对于 web 应用程序,通常对每个请求执行短操作(一般只是显示数据)。 通常不需要在一系列请求间保持 dataset 对象。 对于这类情况,可以使用 sqldatareader。
sqldatareader 对从 sql 数据库检索的数据提供仅向前的只读指针。 因为 sqldatareader 使用表格数据流 (tds) 直接从数据库连接读取数据,因此它如果可以用于方案,其执行效率会比 dataset 高。
若要使用 sqldatareader,请声明 sqlcommand 而不是 sqldataadapter。 sqlcommand 公开返回 sqldatareader 的 executereader 方法。 还请注意,当使用 sqlcommand 时,必须显式打开和关闭 sqlconnection。 调用 executereader 后,sqldatareader 可以绑定到 asp.net 服务器控件,正如将在下一节看到的。
<tab name="c#">
sqlconnection myconnection = new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes"); sqlcommand mycommand = new sqlcommand("select * from authors", myconnection);
myconnection.open();
sqldatareader dr = mycommand.executereader();
...
myconnection.close();
</tab>
<tab name="vb">
dim myconnection as sqlconnection = new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes") dim mycommand as sqlcommand = new sqlcommand("select * from authors", myconnection)
myconnection.open()
dim dr as sqldatareader = mycommand.executereader()
...
myconnection.close()
</tab>
<tab name="jscript">
var myconnection:sqlconnection = new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes"); var mycommand:sqlcommand = new sqlcommand("select * from authors", myconnection);
myconnection.open();
var dr : sqldatareader; dr = mycommand.executereader();
...
myconnection.close();
</tab>
当执行不要求返回数据的命令(如插入、更新和删除)时,也使用 sqlcommand。 该命令通过调用 executenonquery 方法发出,而此方法返回受影响的行数。 注意当使用 sqlcommand 时,必须显式打开连接;sqldataadapter 自动为您处理如何打开连接。
<tab name="c#"> sqlconnection myconnection = new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes"); sqlcommand mycommand = new sqlcommand( "update authors set phone='(800) 555-5555' where au_id = '123-45-6789'", myconnection);
mycommand.connection.open(); mycommand.executenonquery(); mycommand.connection.close();
</tab>
<tab name="vb"> dim myconnection as new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes") dim mycommand as new sqlcommand( _ "update authors set phone='(800) 555-5555' where au_id = '123-45-6789'", _ myconnection)
mycommand.connection.open() mycommand.executenonquery() mycommand.connection.close()
</tab>
<tab name="jscript"> var myconnection:sqlconnection = new sqlconnection("server=(local).netsdk;database=pubs;trusted_connection=yes"); var mycommand:sqlcommand = new sqlcommand( "update authors set phone='(800) 555-5555' where au_id = '123-45-6789'", myconnection);
mycommand.connection.open(); mycommand.executenonquery(); mycommand.connection.close();
</tab>
重要说明:始终记住在页完成执行之前关闭与数据模型的连接。 如果不关闭连接,则可能会在等待页实例被垃圾回收处理期间不经意地超过连接限制。
将 sql 数据绑定到 datagrid
下面的示例显示一个绑定到 datagrid 控件的简单选择查询。 datagrid 呈现包含 sql 数据的表。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器