How do I write string to longvarchar in pervasive larger than 32k?
Question
Application written in VB6. DB is Pervasive v9.5.
Currently works:
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
This works fine until my report or response variable is larger than 32000 characters. I receive this error message when rs.update is called:
"[Pervasive][ODBC Client Interface] String length exceeds column length Parameter #15. Data truncated."
Can anyone point me in the right direction or let me know if I am missing something. Pervasive Longvarchar max size should be 2GB.
Thanks, Graham
La solution
This code works form me using PSQL v11 (I don't have 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"
Basically, you would use parameterized queries rather than the .AddNew method.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow