Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update SQL's varbinary(max) field from VB6

Andrzejek

Programmer
Jan 10, 2006
8,567
US
I have this code in VB6 application - reading PDF file into a byte array:

Code:
Dim aryByte() As Byte
Open App.Path & "\ABC_XYZ.pdf" For Binary Access Read As #1
ReDim aryByte(0 To LOF(1) - 1)
Get #1, , aryByte
Close #1
So, at this time I have my PDF in aryByte variable.

Now I need to update an existing record in SQL table:
Rich (BB code):
UPDATE MyTable
Set Body = ??? aryByte ???
Where ID = 1234

Body field is declared as varbinary(max)

Any trick in updating varbinary field?
 
I did try the Parameterized query, but I could not resolve the adVarBinary line

Code:
Dim cmd As New ADODB.Command
...
strSQL = "UPDATE SampleReport SET " & vbNewLine _
    & " Body                 = ? " & vbNewLine _
    & " WHERE SampleReportID = ?"

With cmd
    .ActiveConnection = CnS
    .CommandText = strSQL
    .Parameters.Append .CreateParameter("@SomeName", adVarBinary, adParamInput, 1234, aryByte)
    .Parameters.Append .CreateParameter("@ID", adInteger, adParamInput, 18, 12345)
    .Execute
End With

But then, I've got this gem: [wiggle]

Rich (BB code):
strSQL = "UPDATE SampleReport SET " & vbNewLine _
    & " Body = (Select BulkColumn From OpenRowset (Bulk '" & YourPath\FileName.pdf & "', Single_Blob) src) " & vbNewLine _
    & " WHERE SampleReportID = 1234 " 
    
CnS.Execute strSQL

I don't really know how it works, it just does. :)
 

Part and Inventory Search

Sponsor

Back
Top