0

此函数位于文件App_Code夹中的 Class 文件中

 Public Shared Function CRUD(ByVal _sql As String, ByVal _parameterNames() As String, ByVal _parameterVals() As String) As Integer
        Dim _noOfRowsAffected As Integer
        Dim _connection As SqlConnection = Global.Connection.GetDbConnection()
        Dim _command As New SqlCommand(_sql, _connection)

        Try
            If _parameterNames IsNot Nothing Then
                For i = 0 To _parameterNames.Length - 1
                    _command.Parameters.AddWithValue(_parameterNames(i), _parameterVals(i))
                Next
            End If

            _noOfRowsAffected = _command.ExecuteNonQuery()
        Catch ex As Exception
            'MsgBox(ex.Message)
            _noOfRowsAffected = -1
        Finally
            If _connection.State = ConnectionState.Open Then
                _connection.Close()
                _connection.Dispose()
                _command.Dispose()
            End If
        End Try

        Return _noOfRowsAffected
    End Function

此代码在 aspx.vb 页面中

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim _parameterNames(6), _parameterVals(6) As String
        _parameterNames(0) = "@Name"
        _parameterVals(0) = txtCoachingName.Text
        _parameterNames(1) = "@Address"
        _parameterVals(1) = txtCoachingAddress.Text
        _parameterNames(2) = "@Mob1"
        _parameterVals(2) = txtCoachingMob1.Text
        _parameterNames(3) = "@Mob2"
        _parameterVals(3) = txtCoachingMob2.Text
        _parameterNames(4) = "@LLine"
        _parameterVals(4) = txtCoachingLLine.Text
        _parameterNames(5) = "@Established"
        _parameterVals(5) = ddlCoachingEstablished.SelectedValue
        _parameterNames(6) = "@DemoVideo"
        _parameterVals(6) = txtCoachingDemoVideo.Text
        _parameterNames(7) = "@Password"
        _parameterVals(7) = txtCoachingPassword.Text
         Try
        DataAccess.CRUD("UPDATE CoachingDetails SET Name=@Name,Address=@Address,Mob1=@Mob1,Mob2=@Mob2,L_Line=@LLine,Established=@Established,Demo_Video=@DemoVideo,Password=@Password,Step_Completed='True',Time_Stamp='" & Date.Now & "'", _parameterNames, _parameterVals)
    Catch ex As Exception

    End Try

现在我想Primary Key Violation在我的页面中捕获异常aspx.vb..但是我没有在aspx.vb页面中得到异常,因为错误被类函数捕获。那么,我如何从类文件中获取异常到 aspx.vb 文件?? 结束子

4

2 回答 2

2

重新抛出。(http://www.selfelected.com/rethrow/)

Try
    ....
Catch ex As Exception 
    do some stuff with ex
    Throw    ' important to not use ex as parameter as that would start a new exception stack.
Finally
    do some other stuff
End Try

由于您在 catch 中不做任何事情,因此请跳过它。

Try
    ....
Finally
    do some other stuff
End Try

为了使代码更好地阅读Using。使用 Using,您可以让编译器为您隐式插入 Try/Finally。代码将类似于:

Using connection As SqlConnection = Global.Connection.GetDbConnection()
    Using command As New SqlCommand(sql, connection)
        If parameterNames IsNot Nothing Then
            For i = 0 To parameterNames.Length - 1
                command.Parameters.AddWithValue(parameterNames(i), parameterVals(i))
            Next
        End If
        noOfRowsAffected = _command.ExecuteNonQuery()
    End Using
End Using

请注意,我将所有变量都设为本地。这对您来说可能是不可能的,因为您对代码的了解比我多得多......(但代码建议您的变量在类变量中,但无论如何您都将它们视为本地变量。)

另请查看Dapper。Dapper 是 Stack overflow 运行的。这是一个不错的小库,用于使编写 sql 调用更容易编写和阅读。

于 2012-07-01T19:05:17.637 回答
1

BCL 没有PrimaryKeyViolationException可以被捕获的。

最接近的方法是捕获SqlException并查看消息以查看 SQL Server 报告的问题是什么。这个Catch块应该在 catch 块之上Exception,因为它更具体。

Try
    If _parameterNames IsNot Nothing Then
        For i = 0 To _parameterNames.Length - 1
            _command.Parameters.AddWithValue(_parameterNames(i), _parameterVals(i))
        Next
    End If

    _noOfRowsAffected = _command.ExecuteNonQuery()
Catch ex As SqlException
    ' Do stuff - logging, checking the exception message etc...
    ' Rethrow exception if you want the exception to bubble up
Catch ex As Exception
    'MsgBox(ex.Message)
    _noOfRowsAffected = -1
Finally
    If _connection.State = ConnectionState.Open Then
        _connection.Close()
        _connection.Dispose()
        _command.Dispose()
    End If
End Try
于 2012-07-01T19:04:29.747 回答