Opening Text file in Binary Mode

Opening Text file in Binary Mode

Post by Dan » Fri, 11 Jun 2004 06:12:39


Hi Guys,
In visual basic, I want to open a text file in binary mode and save the
binary data in to the sql server database (in image coloumn). How to do
this?

Thanks in advance
Dan
 
 
 

Opening Text file in Binary Mode

Post by Alex Ivano » Thu, 17 Jun 2004 04:03:28

Here is a sample.
Sub Test()
Const filename As String = "c:\testsrc.doc"
Const newfile = "c:\testdest.doc"
Dim filedata As String
Dim rs As Recordset

'Store the file in the db
Open filename For Binary As #1
filedata = Space(LOF(1))' reserve the space for Get
Get #1, , filedata
Close #1
Set rs = New Recordset
With rs
.ActiveConnection = "Provider=SQLOLEDB.1;integrated
security=sspi;Initial Catalog=Northwind;Data Source=MyServer"
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open ("select top 0 * from test2")
.AddNew
!ID = 1
![Data].AppendChunk (filedata)
'some providers allow this:
'!Data = filedata
.Update
.Close
End With

'retrieve it back and save with different name
Open newfile For Binary As #1
With rs
.Open ("select * from test2 where id=1")
filedata = !Data.GetChunk(!Data.ActualSize)
Put #1, , filedata
.Close
End With
Close #1
End Sub


--
Please reply to NG only. The email address is not monitored.

Alex.