选择显示字体大小

datasetcommand 对象读取以及更新数据的方式


我们可以利用datasetcommand 对象来执行下列的工作:
1. 将数据源的记录取回,并植入dataset 对象作管理。我们可以利用datasetcommand 对象的filldataset 方法来将取得的数据填入
dataset 对象中。当我们执行这个方法的时候,它会将sql select 的叙述送至数据源。

2. 将datatable 的内容传回数据源。要将dataset 中的datatable 对象所作的变更传回数据源作更新,我们可以使用datasetcommand 对象的update 方法。当我们使用这个方法时,它会将所需要的sql insert、update 或是delete 传回数据源。update 这个方法会检查每一个datarow 的状态,若datarow 是新增加的,该方法就下达insert 的sql 命令;若datarow 有被修改过,该方法就下达update 的sql 叙述;若datarow 被删除,则下达delete 的sql 叙述。

datasetcommand 操作数据源的属性
所以datasetcommand 中有四个属性,而这四个属性其实都是command 对象;分别是selectcommand、insertcommand、updatecommand 以及deletecommand 属性。虽然我们可以明确宣告datasetcommand 中这些对资料源执行更新动作的command 对象,并设定好该command 对象的commandtext 属性,并指定适当的sql 叙述来达到对数据源的insert、update 以及delete 等目的;但是实际上datasetcommand 对象会自动产生它所需要的sql陈述,并不需要我们特别指定。

例如我们将数据从数据源取回,放到dataset 对象中的datatable 对象,其数据表内容如下表所示:

其中datarow 对象中有一个用来表示记录内的数据有无改变的rowstate 属性,预设都是未改变(unchanged)。假设程序将jolin 的usertel 字段内容改掉,其字段状态就会变成已改变(modified),如下表所示:

当我们使用datasetcommand 对象的update 方法,将dataset 的状态更新回数据源时,datasetcommand 对象会去检查datatable 中每一笔记录的rowstate。当dataset 对象检查第一笔和第二笔时,并不会产生任何sql陈述,因为rowstate 属性标示为未改变(unchanged);当检查到第三笔时,因为rowstate 标示为已改变(modified),update 方法会自动产生适当的sql 叙述并且传送到数据源。

使用datasetcommand 对象
datasetcommand 对象可以说是dataset 对象的工作引擎,dataset 和数据源的互动都是由datasetcommand对象来执行;而datasetcommand 则是控制command对象透过connection对象对数据源下命令,和数据源进行互动的工作。以下为datasetcommand 的宣告语法:

dim 变数as datasetcommand = new datasetcommand()

我们先来了解datasetcommand 对象和其它数据操作对象如何搭配使用:

dim strconstr as string = "provider=microsoft.jet.oledb.4.0;" & _
"data source=c:\.netpub\wwwroot\cr\ch05\myweb.mdb"
dim strcomstr as string = "select * from members"
dim cna as adoconnection = new adoconnection(strconstr) '宣告及产生
connection 对象
dim cma as adocommand = new adocommand(strcomstr) '宣告及产生command
对象
dim dsca as adodatasetcommand = new adodatasetcommand() '产生
datasetcommand
cma.activeconnection = cna '指定command 对象cma 要透过cna 这个
connection 对象下命令
dsca.selectcommand = cma '指定datasetcommand 要从数据源取回数据
要透过cma 这个
'command 物件来对connection 下达命令

其中在使用new 运算子建构datasetcommand 时,也可以顺便作初值设定的工作,如下语法所示:

dim 变量as datasetcommand = new datasetcommand(command 对象名)

例如下列范例于宣告command 对象时,直接指定command 对象所要执行的命令,以及要透过哪个connection 对象;并在宣告datasetcommand 时,直接指定所要使用的command 对象名称:

dim strconstr as string = "provider=microsoft.jet.oledb.4.0;" & _
"data source=c:\.netpub\wwwroot\cr\ch05\myweb.mdb"
dim strcomstr as string = "select * from members"
dim cna as adoconnection = new adoconnection(strconstr)
dim cma as adocommand = new adocommand(strcomstr, cna)
dim dsca as adodatasetcommand = new adodatasetcommand(cma)

甚至还可以不需要明确宣告connection 对象以及command 对象,直接以命令文字以及联机字符串来代替,如下语法所示:

dim 变量as datasetcommand = new datasetcommand("命令字符串","联机字符
串")

例如下列范例于宣告datasetcommand 对象时,直接指定datasetcommand 对象所要执行的命令,以及如何建立connection 对象和数据源连结。我们在使用这个datasetcommand 对象时,它会自动建立并管理command 对象以及connection 对象:

dim strconstr as string = "provider=microsoft.jet.oledb.4.0;" & _
"data source=c:\.netpub\wwwroot\cr\ch05\myweb.mdb"
dim strcomstr as string = "select * from members"
dim dsca as adodatasetcommand = new adodatasetcommand(strcomstr,
strconstr)

这样程序就清楚多了。接下来我们就要利用datasetcommand 从数据源取回数据,并填入dataset 对象。以下为宣告的语法:

dim 变量as dataset = new dataset(["dataset 名称"])

要从数据源取回数据并填入dataset 对象,我们利用datasetcommand 对象的filldataset 方法。以下为filldataset 方法的语法:

datasetcommand.filldataset(dataset, "datatable 名称")

我们利用datasetcommand 和数据源联机,它会自动管理connection 对象以及command 对象,所以datasetcommand 使用的connection 对象并不需先用open 方法打开。我们在呼叫datasetcommand 对象的filldataset 时,如果connection 对象没有开启和数据源的连结,datasetcommand 对象会自动呼叫connection 对象的open 方法将对数据源的连结打开;datasetcommand 对数据源的操作执行完毕后,会自动将connection 对象和数据源的连结利用connection 对象的close 方法关闭。如果datasetcommand 在执行filldataset 方法时connection 对象已经开启连结,在执行完毕后datasetcommand 会维持connection 对象原来开启连结的状况。下列范例使用datasetcommand 对象从数据源撷取数据回来,并填入dataset对象中:

<%@import namespace=system.data.ado%>
<%@import namespace=system.data%>
<script language=&quot;vb&quot; runat=&quot;server&quot;>
sub page_load(sender as object, e as eventargs)
dim strconstr as string = &quot;provider=microsoft.jet.oledb.4.0;&quot; & _
&quot;data source=c:\.netpub\wwwroot\cr\ch05\myweb.mdb&quot;
dim strcomstr as string = &quot;select * from members&quot;
dim dsca as adodatasetcommand = new
adodatasetcommand(strcomstr,strconstr)
dim dsdataset as dataset = new dataset()
dsca.filldataset(dsdataset, &quot;members&quot;) '将数据填入数据表内, 并取
名为members
dim shti as short
for shti=1 to (dsdataset.tables(&quot;members&quot;).rows.count).toint16
response.write(dsdataset.tables(0).rows(shti-1)(&quot;username&quot;) &
&quot;<br>&quot;)
next
end sub
</script>

上述范例将数据将所取回的datatable 对象填入dataset 对象中的tables 集合,我们可就可以利用index 或是datatable 名称的方式来取出集合中的对象。取出datatable 对象后,我们可以利用datatable 中rows 集合的count 属性取得总共有几笔记录,并将这些记录全部显示出来。由于rows 集合是由0 开始计算,所以我们最后一个datarow 对象的index 值总是比count属性少1。   


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons