简体   繁体   English

Adodb Recordset函数.MoveNext导致异常“ UPDATE权限被拒绝”

[英]Adodb Recordset function .MoveNext Causes Exception “UPDATE permission was denied”

I've run into a problem today that has stumped me. 我今天遇到一个困扰我的问题。

Here is some sample code: 这是一些示例代码:

                Dim objRec As ADODB.Recordset 
                Dim oRec As ADODB.Recordset 
                Dim oRecBuild As New ADODB.Recordset 
                cmd = New ADODB.Command()
                cmd.ActiveConnection = objConn
                cmd.CommandText = "SelectAll_PhoneNumbers_ById"
                cmd.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
                cmd.NamedParameters = True

                cmd.Parameters.Append(cmd.CreateParameter("@ObjId", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 20, objRec.Fields("PK_ProgramID").Value))

                oRec = New ADODB.Recordset()
                oRec.Open(cmd, , ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)

                If oRec.EOF = False Then
                    Do Until oRec.EOF
                        If IsDBNull(oRec.Fields("PhoneType").Value) = False Then
                            sName = PhoneNumberEdit(oRec.Fields("Phone").Value)
                            If IsDBNull(oRec.Fields("Extension").Value) = False And Len(oRec.Fields("Extension").Value) > 0 Then
                                sName = PhoneNumberEdit(oRec.Fields("Phone").Value) & " " & oRec.Fields("Extension").Value
                            End If
                            oRecBuild.AddNew(New Object() {"TextPrint", "TextType"}, New Object() {sName, oRec.Fields("PhoneType")})
                        End If
                        oRec.MoveNext()
                    Loop
                End If

When I reach the .MoveNext() function the app throws an error that reads like this: The UPDATE permission was denied on the object 'PhoneNumbers', database 'MyDb', schema 'dbo'. 当我到达.MoveNext()函数时,应用程序将引发如下错误:对象“ PhoneNumbers”,数据库“ MyDb”,模式“ dbo”的UPDATE权限被拒绝。

Nothing in this code block is calling an update (the function calls in the loop are just formatting data), does anyone have any idea of what could be causing this? 此代码块中没有什么调用更新(循环中的函数调用仅格式化数据),有人对导致这种情况的原因有任何想法吗?

I should also add that I can run this using SSPI locally, however the code needs to be able to run on a server using a SQL username and PW; 我还应该补充一点,我可以在本地使用SSPI运行此代码,但是代码必须能够在使用SQL用户名和PW的服务器上运行; I have tested updates with the app on other pages, and it works fine. 我已经在其他页面上使用该应用程序测试了更新,并且工作正常。

This is just a hunch, but I do see one place in that code that might result in an UPDATE commend: oRecBuild.AddNew() . 这只是一种预感,但是我确实在该代码中看到一个地方, 可能会引起UPDATE命令: oRecBuild.AddNew() I don't see how it could, but I wonder if calling oRec.MoveNext() is somehow forcing a commit from oRecBuild . 我不知道如何实现,但是我想知道调用oRec.MoveNext()是否以某种方式强制了oRecBuild的提交。 Otherwise, I'd take a longer look at the SelectAll_PhoneNumbers_ById procedure, and any possible triggers that might be attached to tables or views it uses. 否则,我将仔细研究SelectAll_PhoneNumbers_ById过程以及可能附加到它使用的表或视图的任何可能的触发器。

And if that fails, I'd do this: 如果失败了,我会这样做:

Public Class PhoneNumber
    Public Number As String
    Public PhoneType As String
End Public
'...

Dim result As New List(Of PhoneNumber)
Using cn As New SqlConnection("connection string"), _
      cmd As New SqlCommand("SelectAll_PhoneNumbers_ById", cn)

    cmd.CommandType = CommandType.StoredProcedure
    'Is this really an integer?
    cmd.Parameters.Add("@ObjId", SqlDbType.NVarChar, 20).Value = objRec.Fields("PK_ProgramID").Value
    cn.Open()

    Using rdr As SqlDataReader = cmd.ExecuteReader()
       While rdr.Read()
          Dim phone As String = ""
          If Not IsDbNull(rdr("PhoneType"))
              phone = PhoneNumberEdit(rdr("Phone"))
          End If
          If Not IsDbNull(rdr("Extension"))
              phone = phone & " " & PhoneNumberEdit(rdr("Extension"))
          End If
          result.Add(New PhoneNumber With {.Number = phone, .PhoneType = rdr("PhoneType")})
       End While
    End Using      
End Using

While I'm at it... anytime you're reading data from one recordset into another there's a very strong chance that the whole thing could be done entirely in the database, with no client code at all. 当我在这里时……只要您将数据从一个记录集读取到另一个记录集,那么很有可能整个过程可以完全在数据库中完成,而根本没有客户端代码。

To close this question, I wanted to go ahead and say that I finally was able to get this resolved. 为了解决这个问题,我想继续说,我终于能够解决这个问题。 The issue was that dynamic sql being executed with sp_executesql explicitly checks the select, insert, or update privileges of the SQL user; 问题是使用sp_executesql执行的动态sql明确检查SQL用户的选择,插入或更新特权。 execute privs are NOT enough. 执行privs还不够。

So, either the dynamic SQL has to be done away with or the select/update/insert/delete privs must be granted. 因此,要么必须取消动态SQL,要么必须授予select / update / insert / delete privs。

Thank you all for the help and time. 谢谢大家的帮助和时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM