我有例如代码
Dim cmd As New SqlClient.SqlCommand(commandString, databaseString)
Dim result As Integer
Try
result = cmd.ExecuteScalar()
Catch e as Exception
'catch exception code
End Try
除了这样做,我可以覆盖 ExecuteScalar 函数来进行某种通用异常捕获吗?
我有例如代码
Dim cmd As New SqlClient.SqlCommand(commandString, databaseString)
Dim result As Integer
Try
result = cmd.ExecuteScalar()
Catch e as Exception
'catch exception code
End Try
除了这样做,我可以覆盖 ExecuteScalar 函数来进行某种通用异常捕获吗?
您还可以使用组合:
Public Class SqlCommandManager
Private _sqlCommand As System.Data.SqlClient.SqlCommand
Public Sub New(command As System.Data.SqlClient.SqlCommand)
If command Is Nothing Then
Throw New ArgumentNullException("command")
End If
_sqlCommand = command
End Sub
Private ReadOnly Property Command As System.Data.SqlClient.SqlCommand
Get
Return _sqlCommand
End Get
End Property
Public Function ExecuteScalar() As Object
Dim result As Object
Try
result = Command.ExecuteScalar()
Catch e As Exception
'catch exception code
End Try
Return result
End Function
End Class
不是说这是最好的选择,只是说它是一个....
不,但您可以创建一个通用的DoExecuteScalar
辅助函数,该函数采用 SQL 字符串和连接字符串:
Public Function DoExecuteScalar(commandString as String, databaseString as String) as Object
Dim cmd as New SqlClient.SqlCommand(commandString, databaseString)
Dim result as Integer
Try
result = cmd.ExecuteScalar()
Catch e As Exception
Return 0
End Try
Return result
End Function
不,您不能覆盖方法,因为您不能从它继承,因为SqlCommand
is sealed
。
在您使用的地方捕获(或抛出)异常有什么问题SqlCommand
?