⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classes.vb

📁 wrox出版社的另一套经典的VB2005数据库编程学习书籍,收集了书中源码,郑重推荐,电子书,电子书下载
💻 VB
字号:
Option Explicit On
Option Strict On

Imports System.Xml.Serialization
Imports System.IO

Public Class frmClasses
	Private strPath As String
	Private strFile As String
	Private Const strIEFile As String = "\Program Files\Internet Explorer\Iexplore.exe"

	Private Sub btnNorthwind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNorthwind.Click
		'Prove no cheating is going on
		File.Delete("..\Output.xml")

		'Hydrate the Northwind object by deserializing it
		Dim objNwind As New Northwind
		Dim xsInput As New XmlSerializer(objNwind.GetType)
		Dim srInput As New StreamReader("..\Northwind.xml")
		objNwind = CType(xsInput.Deserialize(srInput), Northwind)
		srInput.Close()

		'Change field values to prove the procedure works
		Dim intCtr As Integer
		For intCtr = 0 To objNwind.Customers.Length - 1
			objNwind.Customers(intCtr).CompanyName += " (Edited)"
		Next
		'Same change to Orders
		For intCtr = 0 To objNwind.Orders.Length - 1
			objNwind.Orders(intCtr).ShipName += " (Edited)"
		Next
		'Give everyone a 25% discount
		For intCtr = 0 To objNwind.Order_Details.Length - 1
			objNwind.Order_Details(intCtr).Discount = 0.25D
		Next

		'Dehydrate the Northwind object by serializing it to Output.xml
		Dim xsOutput As New XmlSerializer(objNwind.GetType)
		Dim srOutput As New StreamWriter("..\Output.xml")
		xsOutput.Serialize(srOutput, objNwind)
		srOutput.Close()

		DisplayOutput()
	End Sub

	Private Sub btnNorthwindDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNorthwindDS.Click
		'Prove no cheating is going on
		File.Delete("..\Output.xml")

		'Hydrate the NorthwindDS object by deserializing it
		Dim objNwind As New NorthwindDS
		Dim xsInput As New XmlSerializer(objNwind.GetType)
		Dim fsInput As New FileStream("..\NorthwindDS.xml", FileMode.Open)
		objNwind = CType(xsInput.Deserialize(fsInput), NorthwindDS)
		fsInput.Close()

		'Change field values to prove the procedure works
		Dim intCust As Integer
		Dim intOrder As Integer
		Dim intDetail As Integer
		For intCust = 0 To objNwind.Customers.Length - 1
			'Nested loops that modify fields
			objNwind.Customers(intCust).CompanyName += " (Edited)"
			'Same change to Orders, plus dateTime updates
			For intOrder = 0 To objNwind.Customers(intCust).Orders.Length - 1
				objNwind.Customers(intCust).Orders(intOrder).ShipName += " (Edited)"
				objNwind.Customers(intCust).Orders(intOrder).OrderDate = Today.AddDays(-14 - intOrder)
				'This field isn't serialized
				If objNwind.Customers(intCust).Orders(intOrder).RequiredDateSpecified Then
					objNwind.Customers(intCust).Orders(intOrder).RequiredDate = Today.AddDays(-intOrder)
				End If
				'This field isn't serialized
				If objNwind.Customers(intCust).Orders(intOrder).ShippedDateSpecified Then
					objNwind.Customers(intCust).Orders(intOrder).ShippedDate = Today.AddDays(-7 - intOrder)
				End If
				'Change Discount to 25%
				For intDetail = 0 To objNwind.Customers(intCust).Orders(intOrder).Order_Details.Length - 1
					objNwind.Customers(intCust).Orders(intOrder).Order_Details(intDetail).Discount = 0.25D
				Next
			Next
		Next

		'Dehydrate the NorthwindDS object by serializing it to Output.xml
		Dim xsOutput As New XmlSerializer(objNwind.GetType)
		Dim fsOutput As New FileStream("..\Output.xml", FileMode.Create)
		xsOutput.Serialize(fsOutput, objNwind)
		fsOutput.Close()

		DisplayOutput()
	End Sub

	Private Sub btnCustomersDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomersDS.Click
		'Prove no cheating is going on
		File.Delete("..\Output.xml")

		'Hydrate the Customers object by deserializing it
		Dim objCusts As New Customers
		Dim xsInput As New XmlSerializer(objCusts.GetType)
		Dim fsInput As New FileStream("..\CustomersDS.xml", FileMode.Open)
		objCusts = CType(xsInput.Deserialize(fsInput), Customers)
		fsInput.Close()

		'Change field values to prove the procedure works
		Dim intCust As Integer
		Dim intOrder As Integer
		Dim intDetail As Integer
		For intCust = 0 To objCusts.Customers.Length - 1
			'Nested loops that modify fields (same as NorthwindDS)
			objCusts.Customers(intCust).CompanyName += " (Edited)"
			'Same change to Orders, plus dateTime updates
			For intOrder = 0 To objCusts.Customers(intCust).Orders.Length - 1
				objCusts.Customers(intCust).Orders(intOrder).ShipName += " (Edited)"
				objCusts.Customers(intCust).Orders(intOrder).OrderDate = Today.AddDays(-14 - intOrder)
				'This field isn't serialized
				If objCusts.Customers(intCust).Orders(intOrder).RequiredDateSpecified Then
					objCusts.Customers(intCust).Orders(intOrder).RequiredDate = Today.AddDays(-intOrder)
				End If
				'This field isn't serialized
				If objCusts.Customers(intCust).Orders(intOrder).ShippedDateSpecified Then
					objCusts.Customers(intCust).Orders(intOrder).ShippedDate = Today.AddDays(-7 - intOrder)
				End If
				'Change Discount
				For intDetail = 0 To objCusts.Customers(intCust).Orders(intOrder).Order_Details.Length - 1
					objCusts.Customers(intCust).Orders(intOrder).Order_Details(intDetail).Discount = 0.25D
				Next
			Next
		Next

		'Dehydrate the Customers object by serializing it to Output.xml
		Dim xsOutput As New XmlSerializer(objCusts.GetType)
		Dim srOutput As New StreamWriter("..\Output.xml")
		xsOutput.Serialize(srOutput, objCusts)
		srOutput.Close()

		DisplayOutput()
	End Sub

	Private Sub frmClasses_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If File.Exists(strIEFile) Then
			strPath = Application.StartupPath
			strPath = Mid(strPath, 1, strPath.LastIndexOf("\"))
			strFile = strPath + "\Output.xml"
			'Enable the check box
			chkDisplayWithIE.Enabled = True
		End If
	End Sub

	Private Sub DisplayOutput()
		With chkDisplayWithIE
			If .Enabled And .Checked Then
				Dim strShell As String = """" + strIEFile + """ " + strFile
				Shell(strShell, AppWinStyle.NormalFocus)
			End If
		End With
	End Sub

	Private Sub btnShowEditForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowEditForm.Click
		frmEdit.Show()
	End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -