如何在大于32k的普遍存在的情况下将字符串写入longvarchar?

时间:2012-06-12 20:03:25

标签: vb6 pervasive

用VB6编写的应用程序。 DB是Pervasive v9.5。

目前有效:

Public Sub Save()   
    if rs.State = adStateOpen Then
         rs.AddNew
         SetFields rs
         rs.Update
    End If
end sub

Public Sub SetFields(rs as ADODB.Recordset)
    rs!Name = strName
    StrToField strReport rs!Report
    StrToField strResponse rs!Response
end sub

Public Sub StrToField(ByVal str As String, fld As ADODB.Field)
    Dim Data As String
    Dim StrSize As Long, CharsRead As Long

    ' for field of LONVARCHAR type only
    If fld.Type = adLongVarChar Then
        StrSize = Len(str)
        Do While StrSize <> CharsRead
            If StrSize - CharsRead < BLOCK_SIZE_LONGVARCHAR Then
                Data = Mid(str, CharsRead + 1, StrSize - CharsRead)
                CharsRead = StrSize
            Else
                Data = Mid(str, CharsRead + 1, BLOCK_SIZE_LONGVARCHAR)
                CharsRead = CharsRead + BLOCK_SIZE_LONGVARCHAR
            End If
            fld.AppendChunk Data
        Loop
     Else
        ' do something
     End If
 End Sub

Const BLOCK_SIZE_LONGVARCHAR = 4096

这可以正常工作,直到我的报告或响应变量大于32000个字符。调用rs.update时收到此错误消息:

“[Pervasive] [ODBC客户端接口]字符串长度超出列长度参数#15。数据被截断。”

任何人都可以指出我正确的方向或让我知道我是否遗漏了什么。 Pervasive Longvarchar最大大小应为2GB。

谢谢, 格雷厄姆

1 个答案:

答案 0 :(得分:0)

此代码使用PSQL v11(我没有v9.5)。

Dim conn As New ADODB.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = "demodata"
conn.Open
Dim sql As String
Dim cmd As New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandText = "insert into lvc (f2) values (?)"
Dim parm As New ADODB.Parameter
parm.Name = "f2"
Dim longstring As String
Open "c:\longdata.txt" For Input As #1
Do While Not EOF(1)
   Line Input #1, sNextLine
   'do something with it
   'add line numbers to it, in this case!
   sText = sText & sNextLine
Loop
longstring = sText
parm.Value = longstring
cmd.Parameters.Append cmd.CreateParameter("param1", adLongVarChar, adParamInput, Len(longstring), longstring)
cmd.Execute
conn.Close
MsgBox "done"

基本上,您将使用参数化查询而不是.AddNew方法。