【问题标题】:passing recordset to stored procedure in MS access将记录集传递给 MS 访问中的存储过程
【发布时间】:2017-08-29 14:30:01
【问题描述】:

所以,我目前正在开发一个应用程序,我需要调用一个将用户定义的表作为参数的存储过程。在 .NET 中,我只能在数据表中传递数据,但在 MS Access VBA 中,我很难将 ADO 记录集作为用户定义的表传递给存储过程。

Private Sub DeleteRecords()
On Error GoTo Err_DeleteRecords

Dim cnn As ADODB.Connection, cmd As New ADODB.Command, param As New ADODB.Parameter

Dim i As Integer
Dim rs As ADODB.Recordset
Dim text As String
Dim gs As String

Set cnn = CurrentProject.Connection

gs = "DECLARE @StagingRecsToDelete StagingRecsToDelete_UDT; SELECT * FROM @StagingRecsToDelete"

Set rs = New ADODB.Recordset

rs.Open gs, cnn, adOpenKeyset, adLockOptimistic

If rs.RecordCount = 0 Then
    For i = 0 To Me.lstFingerPrintServiceStaging.ListCount - 1
        If Me.lstFingerPrintServiceStaging.Selected(i) Then
             text = text + Me.lstFingerPrintServiceStaging.Column(0, i)
             rs.AddNew
             rs!intFingerPrintServiceStagingID = text
        End If
    Next i
End If

If rs.RecordCount > 0 Then
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = cnn
    cmd.CommandType = adCmdStoredProc
    cmd.CommandText = "dbo.SPFingerPrintDeleteErrors"

    Set param = cmd.CreateParameter("@ReturnValue", adInteger, adParamReturnValue)
    cmd.Parameters.Append param
    Set param = cmd.CreateParameter("StagingRecsToDelete_UDT", adVarient, adParamInput, , rs)
    cmd.Parameters.Append param
    Set param = cmd.CreateParameter("@ErrNbr", adInteger, adParamOutput)
    cmd.Parameters.Append param
    Set param = cmd.CreateParameter("@ErrMsg", adVarChar, adParamOutput, 8000)
    cmd.Parameters.Append param

    cmd.Execute
End If

Select Case cmd.Parameters("@ReturnValue").Value
    Case 1 'System error
        MsgBox cmd.Parameters("@ErrMsg").Value, vbOKOnly + vbInformation, gApplicationTitle
        Exit Sub
    Case 0 'Succeeds
        MsgBox "The Selected records have been successfully deleted.", vbOKOnly + vbInformation, gApplicationTitle
    Case -1 'User Error
        MsgBox cmd.Parameters("@ErrMsg").Value, vbOKOnly + vbInformation, gApplicationTitle
        Exit Sub
End Select

cnn.Close

Set cnn = Nothing

Exit_DeleteRecords:
 Exit Sub

Err_DeleteRecords:
 MsgBox "Form=" & Me.Name & ", Function=DeleteRecords" & vbCrLf & "Err#=" & 
Err & " " & Error$
 Resume Exit_DeleteRecords
End Sub

在大多数情况下,所有这些都有效,但是当我将 UDT 传递给存储过程时,我不知道将它设置为什么数据类型。

【问题讨论】:

  • 我不相信经典 ADO 支持表值参数。相反,您可以将行插入临时表并随后使用它。
  • 是的,如果 proc 接受逗号分隔的字符串会更容易,但是想要尝试将其作为 UDT 保留在 proc 中的权力。

标签: sql-server vba ms-access stored-procedures


【解决方案1】:

MS Access (DAO) 不支持它。

您可以使用您的数据创建一个 XML 并将其作为 SP 参数传递或将其一一插入到临时表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 2011-07-28
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多