📄 datasetform.vb
字号:
Imports System.Text
Public Class DataSetForm
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
DoStuffWithDataSet()
End Sub
Private Sub DoStuffWithDataSet()
' Create an instance of the strongly-typed DataSet
Dim productsDS As ProductsDataSet = New ProductsDataSet()
' Create a table adapter for product categories
Dim catTA As ProductsDataSetTableAdapters.ProductCategoryTableAdapter = _
New ProductsDataSetTableAdapters.ProductCategoryTableAdapter()
' ... and for products
Dim prodTA As ProductsDataSetTableAdapters.ProductTableAdapter = _
New ProductsDataSetTableAdapters.ProductTableAdapter()
' FIRST: Get any existing data in these tables from the database
' Fill the ProductCategories table in the DataSet with data from the database
catTA.Fill(productsDS.ProductCategory)
' .. and the Products.
prodTA.Fill(productsDS.Product)
' Report count of records to the screen...
ReportRecords(productsDS)
' SECOND: Add some product categories
' To make sure we don't duplicate names, productCategory.Name has a unique constraint
Try
productsDS.ProductCategory.AddProductCategoryRow("Rock Climbing Equipment")
productsDS.ProductCategory.AddProductCategoryRow("Scuba Diving Equipment")
' Update to write changes back to the database
catTA.Update(productsDS.ProductCategory)
textBox1.Text += "\r\nProduct Categories added.\r\n"
Catch
' If the categories already exist, just continue
textBox1.Text += "\r\nProduct Category addition failed, items already exist.\r\n"
End Try
' THIRD: Add some products in the category with categoryID of 1
Dim prodCatRow As ProductsDataSet.ProductCategoryRow = Nothing
For Each row As ProductsDataSet.ProductCategoryRow In productsDS.ProductCategory.Rows
If row.ProductCategoryID = 1 Then
prodCatRow = row
Exit For
End If
Next
System.Diagnostics.Debug.Assert(Not prodCatRow Is Nothing, "Logic erro: No ProductCategory '1' found")
productsDS.Product.AddProductRow("Contoso Single Rope", "Red/Blue", 155.95, "60m", prodCatRow)
productsDS.Product.AddProductRow("Contoso Rock Shoes", "Black", 89.95, "8", prodCatRow)
' Write to database
prodTA.Update(productsDS.Product)
textBox1.Text += "\r\nProducts added.\r\n"
' Report count of records to screen...
ReportRecords(productsDS)
End Sub
Private Sub ReportRecords(ByVal productsDS As ProductsDataSet)
Dim sb As StringBuilder = New StringBuilder(textBox1.Text)
sb.Append("There are currently ")
sb.Append(productsDS.ProductCategory.Rows.Count)
sb.Append(" product categories.\r\n")
sb.Append("There are currently ")
sb.Append(productsDS.Product.Rows.Count)
sb.Append(" products.\r\n")
textBox1.Text = sb.ToString()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -