addressbasic.vb

来自「wrox出版社的另一套经典的VB2005数据库编程学习书籍,收集了书中源码,郑重」· VB 代码 · 共 183 行

VB
183
字号
Imports System
Imports System.Data.SqlTypes
'Imports System.Data.SqlServer (not required for UDTs)
Imports System.Data.Sql
Imports Microsoft.SqlServer.Server
Imports System.IO

'Actual MaxByteSize = 40 + 60 + 15 + 15 + 10 + 15 = 155 * 2 = 310 (UTF-16)
<Serializable(), SqlUserDefinedType(Format.UserDefined, _
 IsByteOrdered:=True, IsFixedLength:=False, MaxByteSize:=500)> _
Public Class AddressBasic
    'The AddressBasic UDT expects zero-length strings instead of nulls 
	'for Region and PostalCode fields
	Implements INullable, IBinarySerialize

	Private blnIsNull As Boolean
	Private strName As String
	Private strAddress As String
	Private strCity As String
	Private strRegion As String
	Private strPostalCode As String
	Private strCountry As String

	Public Sub New()
		'Default constructor (required for a class)
		Me.blnIsNull = True
		Me.strName = ""
		Me.strAddress = ""
		Me.strCity = ""
		Me.strRegion = ""
		Me.strPostalCode = ""
		Me.strCountry = ""
	End Sub

	Public ReadOnly Property IsNull() As Boolean _
	 Implements INullable.IsNull
		'IsNull property is required for all UDTs
		Get
			Return blnIsNull
		End Get
	End Property

	Public Shared ReadOnly Property Null() As AddressBasic
		'Null property is required for all UDTs
		Get
			Dim objAddr As New AddressBasic()
			objAddr.blnIsNull = True
			Return objAddr
		End Get
	End Property

	Public Shared Function Parse(ByVal sqlAddr As SqlString) As AddressBasic
		'Parse is required for all UDTs
		If sqlAddr.IsNull Then
			Return Nothing
		Else
			Dim objAddr As New AddressBasic
			Dim str As String = sqlAddr.ToString
			Dim astrAddr As String() = str.Split(";"c)
			objAddr.strName = astrAddr(0)
			objAddr.strAddress = astrAddr(1)
			objAddr.strCity = astrAddr(2)
			objAddr.strRegion = astrAddr(3)
			objAddr.strCountry = astrAddr(5)
			objAddr.blnIsNull = False
			Return objAddr
		End If
	End Function

	Public Overrides Function ToString() As String
		'ToString is required for all UDTs
		If Me.IsNull Then
			Return "Null"
		Else
			Dim strDelimeter As String = ";"
			Return Me.strName + strDelimeter + Me.strAddress + strDelimeter + Me.strCity + strDelimeter + Me.strRegion + strDelimeter + Me.strPostalCode + strDelimeter + Me.strCountry
		End If
	End Function

	'Fields
	Public Property Name() As String
		Get
			Return Me.strName
		End Get
		Set(ByVal value As String)
			Me.strName = value
			Me.blnIsNull = False
		End Set
	End Property

	Public Property Address() As String
		Get
			Return Me.strAddress
		End Get
		Set(ByVal value As String)
			Me.strAddress = value
			Me.blnIsNull = False
		End Set
	End Property

	Public Property City() As String
		Get
			Return Me.strCity
		End Get
		Set(ByVal value As String)
			Me.strCity = value
			Me.blnIsNull = False
		End Set
	End Property

	Public Property Region() As String
		Get
			Return Me.strRegion
		End Get
		Set(ByVal value As String)
			Me.strRegion = value
			Me.blnIsNull = False
		End Set
	End Property

	Public Property PostalCode() As String
		Get
			Return Me.strPostalCode
		End Get
		Set(ByVal value As String)
			Me.strPostalCode = value
			Me.blnIsNull = False
		End Set
	End Property

	Public Property Country() As String
		Get
			Return Me.strCountry
		End Get
		Set(ByVal value As String)
			Me.strCountry = value
			Me.blnIsNull = False
		End Set
	End Property

	'Required serializer and deserializer
	Public Overridable Sub Write(ByVal binWriter As BinaryWriter) _
	 Implements IBinarySerialize.Write
		Dim bytHeader As Byte
		If Me.IsNull Then
			bytHeader = 0
		Else
			bytHeader = 1
		End If
		With binWriter
			.Write(bytHeader)
			If bytHeader = 0 Then
				Return
			End If
			.Write(Me.Name)
			.Write(Me.Address)
			.Write(Me.City)
			.Write(Me.Region)
			.Write(Me.PostalCode)
			.Write(Me.Country)
		End With
	End Sub

	Public Sub Read(ByVal binReader As BinaryReader) _
	 Implements IBinarySerialize.Read
		'Required for Format.UserDefined, not Format.Native
		Dim bytHeader As Byte = binReader.ReadByte()
		If bytHeader = 0 Then
			Me.blnIsNull = True
			Return
		End If
		Me.blnIsNull = False
		With binReader
			Me.strName = .ReadString()
			Me.strAddress = .ReadString()
			Me.strCity = .ReadString()
			Me.strRegion = .ReadString()
			Me.strPostalCode = .ReadString()
			Me.strCountry = .ReadString()
		End With
	End Sub
End Class

⌨️ 快捷键说明

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