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類就可以了,
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”,運行程式,則可以對記錄進行編輯了
<?xml version="1.0" standalone="yes"?>
<Books xmlns="http://tempuri.org/Books.xsd">
<Book>
<Title>ASP.NET 2.0: A Developer's Notebook (O'Reilly)
</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 (O'Reilly)
</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控制項,點'Configure Data Source…’連結,設定其資料來源為msdn.xml,在xpath運算式中,設定為“rss/channel/item”,則只返回item結點下的內容,再拖拉一個datalist控制項,將其資料來源設定為xmldatasource。