2012-07-12 3 views
0

나는 SqlCommand 함수를 재정의 할 수 있습니까?

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는 기능을 대체 할 수 있다는 일이 될 것입니다 코드가?

+0

당신은 일반적인 예외의 일종을하는 개인 방법을 쓸 수있다 다만,이 최선의 대안이다라고하지 잡아서 대신 호출하십시오. –

답변

1

아니,하지만 당신은 SQL 문자열과 연결 문자열을 사용하는 일반 DoExecuteScalar 도우미 함수 만들 수 있습니다 SqlCommandsealed 때문에 당신이 그것을 상속 할 수 없기 때문에

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 
1

이 아니 당신이 메소드를 오버라이드 (override) 할 수 있습니다.

SqlCommand을 사용하는 곳에서 예외를 포착하는 (또는 던지기) 예외는 무엇입니까?

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 

는 하나 ....

관련 문제