By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

Visual Basic Tip of the Week #2

Expert 5K+
P: 8,434
Originally posted by sashi

BLOB - Save image to database

Expand|Select|Wrap|Line Numbers
  1. Dim CN As New ADODB.Connection
  2. Dim RS As ADODB.Recordset
  3. Dim DataFile As Integer, Fl As Long, Chunks As Integer
  4. Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String
  5.  
  6. Private Const ChunkSize As Integer = 16384
  7. Private Const conChunkSize = 100
  8.  
  9. Private Sub cmdSave_Click()
  10.     CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
  11.     Dim strSQL As String
  12.  
  13.     strSQL = "SELECT * FROM pub_info where pub_id = '9999'"
  14.     RS.Open strSQL, CN, adOpenForwardOnly, adLockOptimistic
  15.  
  16.     RS.AddNew
  17.       SavePicture
  18.     RS.Update
  19.  
  20.     Set RS = Nothing
  21.     Set RS = New Recordset
  22. End Sub
  23.  
  24. Private Sub SavePicture()
  25.     Dim strFileNm As String
  26.     DataFile = 1
  27.     Open strFileNm For Binary Access Read As DataFile
  28.         Fl = LOF(DataFile)   ' Length of data in file
  29.         If Fl = 0 Then Close DataFile: Exit Sub
  30.         Chunks = Fl \ ChunkSize
  31.         Fragment = Fl Mod ChunkSize
  32.         ReDim Chunk(Fragment)
  33.         Get DataFile, , Chunk()
  34.         RS!logo.AppendChunk Chunk()
  35.         ReDim Chunk(ChunkSize)
  36.         For i = 1 To Chunks
  37.             Get DataFile, , Chunk()
  38.             RS!logo.AppendChunk Chunk()
  39.         Next i
  40.     Close DataFile
  41. End Sub
  42.  
Apr 17 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.