0

たとえば、次のようなコードがあります

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関数をオーバーライドして、ある種の一般的な例外キャッチを実行できますか?

4

3 に答える 3

1

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
于 2012-07-12T19:51:18.643 に答える
1

いいえ、メソッドから継承できないため、メソッドをオーバーライドすることはできませSqlCommandsealed

を使用している場所で例外をキャッチ(またはスロー)することの何が問題になっていますSqlCommandか?

于 2012-07-12T19:51:34.803 に答える
1

コンポジションを使用することもできます。

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

これが最良の選択肢であると言っているのではなく、それが1つであるというだけです...。

于 2012-07-13T01:06:13.660 に答える