【发布时间】:2020-01-27 18:44:34
【问题描述】:
我不是很精通来自 c# 背景的 vb.net。我有一个带有 btn 和输入的表单,它接受一个字符串并通过存储的过程在数据库中运行和更新。存储过程应该返回一个@Message 和@Success 参数。但是,我有点困惑如何取回这些参数。我有 3 个功能。 btn click sub(调用BL函数)、BL函数(调用DA函数)、DA函数(调用sql server中存储的proc)。我也对整个 byref 的工作方式感到困惑。我读到我不必从我的 DA 函数返回任何内容,但如果这是真的,那么 BA 函数将如何接收成功/消息参数?
我知道我在这里错过了很多。有人可以帮我重新构建它,并简要解释它是如何/为什么这样工作的。下面是我的代码。
BTN 点击子:
'Button click that calls CertificateOrder BL function InvalidateCertificate()
Private Sub btnInvalidateCertificate_Click(sender As Object, e As System.EventArgs) Handles btnInvalidateCertificate.Click
'Cert ID to invalidate
Dim certificateId As String = txtCertificateId.Value
Dim msg As Response.BaseResponse
'Call stored proc from BL
msg = CertificateOrder.InvalidateCertificate(certificateId)
Me.txtCertificateId.ErrorText = msg.Message
If msg.Success Then
Forms.FadeForm.ShowDialog(msg.Message, "Success", Forms.FadeForm.MessageIcons.Information, Forms.FadeForm.MessageButtons.Ok)
End If
End Sub
BL功能:
''' <summary>
''' Calls DataAccess function InvalidateCertificate()
''' to invalidate certificate
''' </summary>
Public Shared Function InvalidateCertificate(ByVal certificateId As System.String) As Response.BaseResponse
Using context As New CORADBContext
Dim message As System.String = ""
Dim success As System.Boolean = False
context.InvalidateCertificate(certificateId, success, message)
Return message
End Using
End Function
DA功能:
''' <summary>
''' Calls the [spInvalidateCertificate] stored proc
''' to update a cert to be invalid
''' </summary>
''' <returns></returns>
<Extension>
Public Function InvalidateCertificate(ByVal context As DbContextBase,
ByVal certificateId As System.String,
ByRef success As System.Boolean,
ByRef message As System.String) As Int32
Dim successParameter As New SqlParameter("@Success", success) With {.Direction = ParameterDirection.InputOutput, .Value = False}
Dim messageParameter As New SqlParameter("@Message", message) With {.Direction = ParameterDirection.InputOutput, .Value = ""}
Dim parameters() As SqlParameter = {New SqlParameter("@CertificationValue", certificateId), successParameter, messageParameter}
Dim results As Int32 = context.ExecuteProcedure("Orders.spInvalidateCertificate", parameters)
success = DirectCast(successParameter.Value, System.Boolean)
message = DirectCast(messageParameter.Value, System.String)
'success and message need to be returned
Return 0
End Function
【问题讨论】:
-
什么是
Response.BaseResponse?这是该项目中的自定义对象吗?代码期望它返回给 UI 事件处理程序,但下游 BL 代码返回的是字符串而不是这种类型的对象。 -
DA 函数
InvalidateCertificate应该返回success和message的值,整齐地包装在一个数据结构中(即在一个类中)。它真的不应该操纵通过引用传递的参数。我认为这也许是Response.BaseResponse对象的意图?
标签: vb.net stored-procedures byref return-by-reference