欢迎光临
我们一直在努力

ASP.NET 2.0中的DataSource系列控件-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

asp.net 2.0中,在其中的数据连接方面做了很大的改进,新加入的datasource系列控件,使得在数据库的连接方面更加容易,很多都可以通过向导式的设置来完成sql语句的编写和数据库连接。asp.net 2.0中的datasource系列控件总共有6种,分别是:

  sqldatasource控件—-用于连接sql数据库的数据源控件
  accessdatasource控件—-用于连接access数据库的数据源控件
  objectdatasource控件—-用于连接自定义对象的数据源控件
  datasetdatasource控件—–将xml文件做为dataset并进行相关处理的控件
  xmldatasource控件—–该控件装载xml文件,并绑定到datagrid、datalist等控件中
  sitemapdatasource控件—–该控件装载一个预先定义好的站点布局文件,之后将其与treenode树形控件或sitemappath控件绑定,以实现方便地制作站点的页面导航功能。

 
  本文中,将重点介绍objectdatasource控件,datasetdatasource控件和xmldatasource控件,而sqldatasource控件的介绍,请参考《使用asp.net 2.0中的gridview控件》,该文中介绍了sqldatasource控件的使用方法,而accessdatasource控件,则与sqldatasource 控件类似,只不过连接的数据库是access。

  objectdatasource控件

  该控件,将用户自己创建的对象绑定到数据控件中,比如绑定到datagrid,gridview。下面来看个例子,在visual studio 2005 beta 1中,创建新的站点,并添加一个新的类,名称叫products:

imports microsoft.visualbasic
imports system.data
imports system.data.sqlclient

public class products
public function getproducts() as dataset
 dim conn as new sqlconnection(“server=(local);integrated security=true;database=northwind;persist
security info=true”)
 dim adapter as new sqldataadapter(“select [productid], [productname], [supplierid], [categoryid],
[quantityperunit], [unitprice] from [products]”, conn)

 dim ds as new dataset
 adapter.fill(ds, “products”)
 return ds
end function
end class

  product类包含了getproducts方法,该方法返回northwind数据库中所有的产品,以dataset形式返回。使用objectdatasource 控件,可以将自定义的类绑定到数据控件中,而只需要将ojectdatasource 控件拖拉到设计窗体中,之后,点击configure data source…链接,在弹出的窗体中(如下图),选择要绑定的类,此时选择product类就可以了,

ASP.NET 2.0中的DataSource系列控件-.NET教程,Asp.Net开发

  在下一步中,选择要绑定哪一个类中的相关方法

 

  在下一步中,将可以选择执行什么样的sql语句,比如select,update,insert,delete等操作,本文中只需要返回product数据,所以选择select就可以了,之后点finish完成操作。

  接着,拖拉一个gridview控件到窗体中,将其绑定到刚才我们创建的objectdatasource 控件,并将enable paging, enable scripting, enable selection三个选择框打勾,如下图:

ASP.NET 2.0中的DataSource系列控件-.NET教程,Asp.Net开发

  之后运行程序,就可以看到结果。如果要对ojectdatasource 控件进行编辑的话,就要另外提供一个方法了,我们加入一个叫updateproducts的方法,如下:

public sub updateproducts(byval productid as integer, byval productname as string, _
 byval supplierid as integer, byval categoryid as integer, _
 byval quantityperunit as string, byval unitprice as double)
 dim conn as new sqlconnection(“server=(local);integrated security=true;database=northwind;persist security
info=true”)
 dim adapter as new sqldataadapter(“select * from products where productid=” & productid, conn)
 dim ds as new dataset
 adapter.fill(ds, “products”)
 with ds.tables(0).rows(0)
  .item(“productname”) = productname
  .item(“supplierid”) = supplierid
  .item(“categoryid”) = categoryid
  .item(“quantityperunit”) = quantityperunit
  .item(“unitprice”) = unitprice
 end with
 dim cb as new sqlcommandbuilder(adapter)
 adapter.update(ds, “products”)
end sub

  之后再绑定到objectdatasource控件,并选用其中的update选项卡中的updateproducts方法,并在绑定到gridview控件时,选择“enable editing option”,运行程序,则可以对记录进行编辑了

datasetdatasource控件

  该控件允许将xml document或其他文件看作dataset进行处理,比如有一个xml文件如下,以books.xml文件命名:

<?xml version=”1.0″ standalone=”yes”?>
<books xmlns=”http://tempuri.org/books.xsd”>
<book>
<title>asp.net 2.0: a developers notebook (oreilly)
</title>
<pubdate>december 2004</pubdate>
<synopsis>to bring you up to speed with asp.net 2.0, this practical book offers nearly 50 hands-on projects.
.</synopsis>
</book>
<book>
<title>.net compact framework pocket guide (oreilly)
</title>
<pubdate>may 2004</pubdate>
<synopsis>looking to create applications for pocket pc and windows based smartphones? </synopsis>
</book>
</books>

  下面,将使用datasetdatasource控件,将xml文件绑定到gridview中。将datasetdatasource控件拖拉到设计窗体,并选“configure data source”,在数据源设置窗体中,选择books.xml作为数据源,再拖拉一个gridview控件,将其绑定到datasetdatasource控件中

 

  xmldatasource控件
 
  该控件也允许将xml document或其他文件绑定到datagrid,gridview中,但被绑定的xml文件的结构可以是不大规则的,不包含dataset。xmldatasource控件还可以使用xpath,可以将xml文件绑定到treeview等其他控件中去。比如一个rss的文件,其xml表示如下,保存为msdn.xml:

  ASP.NET 2.0中的DataSource系列控件-.NET教程,Asp.Net开发

  拖拉一个xmldatasource控件,点configure data source…’链接,设置其数据源为msdn.xml,在xpath表达式中,设置为“rss/channel/item”,则只返回item结点下的内容,再拖拉一个datalist控件,将其数据源设置为xmldatasource。

  在smart tag菜单中,选择“auto format…”,并选择slate scheme,再切换到代码窗口,增加如下的代码:

<asp:datalist id=”datalist1″ runat=”server”
gridlines=”horizontal”
borderwidth=”1px” backcolor=”white” cellpadding=”3″
borderstyle=”none” bordercolor=”#e7e7ff”
datasourceid=”xmldatasource1″>
<footerstyle forecolor=”#4a3c8c”
backcolor=”#b5c7de”></footerstyle>
<itemtemplate>
<b><%#xpath(“title”)%></b><br />
<i><%#xpath(“pubdate”)%></i><br />
<%#xpath(“description”)%><br />
<a href=<%#xpath(“link”)%>>link</a><br />
<br />
</itemtemplate>
<alternatingitemstyle backcolor=”#f7f7f7″>
</alternatingitemstyle>
<itemstyle forecolor=”#4a3c8c”
backcolor=”#e7e7ff”>
</itemstyle>
<selecteditemstyle forecolor=”#f7f7f7″
font-bold=”true”
backcolor=”#738a9c”></selecteditemstyle>
<headertemplate>rss feeds</headertemplate>
<headerstyle forecolor=”#f7f7f7″
font-bold=”true”
backcolor=”#4a3c8c”></headerstyle>
</asp:datalist>

  运行,就可以看到一个简单的rss形式的阅读器了,如下图,十分方便。

ASP.NET 2.0中的DataSource系列控件-.NET教程,Asp.Net开发

  至于sitemapdatasource控件,请参考《在asp.net 2.0中使用页面导航控件》一文

  总结:

  本文简单介绍了asp.net 2.0中十分强大的新增的datasource系列控件,有了这些控件,在与数据库和其他数据源的相关操作中,将十分方便,不用编写太多的代码,在正式版的asp.net 2.0中,估计会增加更多的功能。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.NET 2.0中的DataSource系列控件-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章