请看下面的代码:
Imports System.Data.SqlClient
Public Class Person
Private id As String
Private name As String
Public Function check(ByVal personid As String) As Boolean
'Do some checks on the person to see if it is ready for deletion
End Function
Public Shared Sub Delete()
Dim v As New Vehicle
Dim p As New Person
End Sub
End Class
Public Class Vehicle
Private vrm As String
Public Function check(ByVal vehicleid As String) As Boolean
'Do some checks on the vehicle to see if it is ready for deletion
End Function
Private Shared Sub Delete()
Dim p As New Person
Dim v As New Vehicle
Dim objCommand As SqlCommand
Dim objCon As SqlConnection
Dim objDR As SqlDataReader
Try
Dim _ConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=Test;Integrated Security=True;MultipleActiveResultSets=true"
objCon = New SqlConnection(_ConString)
objCommand = New SqlCommand("SELECT * FROM Person were startdate < dateadd(year,-6," & Now & ")")
objDR = objCommand.ExecuteReader
Do While objDR.Read
If p.check(objDR("id")) And v.check(objDR("vehicleid")) Then
'Execute delete statement, which deletes the person and vehicle
End If
Loop
objDR.Close()
objCommand.Connection = objCon
objCon.Open()
objCommand.ExecuteNonQuery()
Catch ex As Exception
Throw
Finally
End Try
End Sub
End Class
请注意,共享函数 (Person.Delete) 包含对 Person 和 Vehicle 的引用,并在 Person 和 Vehicle 中使用实例变量。基本上,在删除人员和车辆之前,需要对人员和车辆进行检查。
从 Shared 函数中引用 Person 和 Vehicle 是不好的做法吗?在 Delete 函数中使用实例变量是不好的做法。删除功能每天将删除成千上万的人和车辆。