此功能位于App_Code
文件夹
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文件?
结束子
答案 0 :(得分:2)
Rethrow。 (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
由于你没有在捕获中做任何事情,只需跳过它。
Try
....
Finally
do some other stuff
End Try
要使代码更好,请在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溢出运行的东西。这是一个很好的小型库,可以让编写sql调用更容易编写和读取。
答案 1 :(得分:1)
BCL没有可以被捕获的PrimaryKeyViolationException
。
最接近的是捕获SqlException
并查看消息以查看SQL Server报告的问题。对于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
的阻止块,因为它更具体。
{{1}}