Can't read data from varchar(max) using ADODB

回眸只為那壹抹淺笑 提交于 2019-12-11 03:16:14

问题


I've got a method that uses ADODB to execute a sproc and read the results as a recordset. Everything was working fine until i changed one of the output fields from varchar(2000) to varchar(max) (SQL2008). Now I can't read the data from that field. The strange thing is that the data is visible in the debugger immediately after running the Execute, but stepping in the debugger makes the data vanish.

Here is the code:

Public Function sproc_RptEAComment(ByVal accountName As String, ByVal contractName As String,
                                            ByVal acctType As String, ByVal asOfDate As DateTime,
                                            ByVal sessionID As String, ByVal clin As String,
                                            ByVal dollarDisplay As String) As List(Of sproc_RptEAComment_Row) Implements ISprocRepository.sproc_RptEAComment
    Try

        Dim cmd = New Command()
        cmd.ActiveConnection = Connection
        cmd.CommandType = CommandTypeEnum.adCmdStoredProc
        cmd.CommandText = "sproc_RptEAComment"

        ADOUtilities.AddParamToSproc(cmd, "@ChargeNum", accountName)
        ADOUtilities.AddParamToSproc(cmd, "@Contract", contractName)
        ADOUtilities.AddParamToSproc(cmd, "@EmployeeName", "")
        ADOUtilities.AddParamToSproc(cmd, "@Org", acctType)
        ADOUtilities.AddParamToSproc(cmd, "@HoursVal", "TRUE")
        ADOUtilities.AddParamToSproc(cmd, "@Sort", "1")
        ADOUtilities.AddParamToSproc(cmd, "@Employer", "")
        ADOUtilities.AddParamToSproc(cmd, "@Type", "1")
        ADOUtilities.AddParamToSproc(cmd, "@FromD", asOfDate.ToShortDateString())
        ADOUtilities.AddParamToSproc(cmd, "@ToD", asOfDate.AddMonths(-5).ToShortDateString()) 
        ADOUtilities.AddParamToSproc(cmd, "@SessionID", sessionID)
        ADOUtilities.AddParamToSproc(cmd, "@Clin", clin)
        ADOUtilities.AddParamToSproc(cmd, "@RptDisplay", "")
        ADOUtilities.AddParamToSproc(cmd, "@Parm_DT", dollarDisplay)

        Dim lst = New List(Of sproc_RptEAComment_Row)
        Dim rs = cmd.Execute()
        While Not (rs.EOF)
            Dim newEntity = New sproc_RptEAComment_Row(rs)
            lst.Add(newEntity)
            rs.MoveNext()
        End While
        Return lst

    Catch ex As Exception
        MsgLogger.Err(ex)
        Return Nothing
    End Try
End Function

If I look in the debugger immediately after the Execute I see this. Note the field EacJustCom has the proper string value:

I take one step in the debugger and see this. THe value is gone. Note the field "_Account" is still intact (it's defined as varchar(100)):


回答1:


The problem you are having is that a varchar(max) can hold up to 2 GB of data.

In the old days with Visual Basic and DAO, there was a method call GetChunk() to read bits of data from a binary field and AppendChunk() to write bits. Use a binary or text file stream to put the data back together on the client side.

"Use the GetChunk method on a Field object to retrieve part or all of its long binary or character data."

See the MSDN reference.

These feilds have to be handled differently than a regular record set. Exclude them from your record set.

Check out this article Who is afraid of the big bad blob?

Since there are so many development environments, vs xxx or ADO or ADO.NET, it is difficult to point out your particular issues w/o more information.

Write back to me if this helps ...




回答2:


I never found the answer to the problem, but did find a workaround. If I create the recordset first and fill it from the command, it works.

        Dim rs As New ADODB.Recordset()
        rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
        rs.CursorType = ADODB.CursorTypeEnum.adOpenForwardOnly
        rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
        rs.StayInSync = False
        rs.Open(cmd)
        rs.ActiveConnection = Nothing

        Dim lst = New List(Of sproc_RptEAComment_Row)
        While Not (rs.EOF)
            Dim newEntity = New sproc_RptEAComment_Row(rs)
            lst.Add(newEntity)
            rs.MoveNext()
        End While
        Return lst


来源:https://stackoverflow.com/questions/18659969/cant-read-data-from-varcharmax-using-adodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!