Microsoft Visual Basic training - Saving/retrieving Media Files into/from Database

How to work with media file in Visual Basic with a Database


vby6 home

BLOBs vb6

How to manipulate media files "BLOBs" using Visual Basic with a Database end.

Introduction

What is BLOB?

BLOB is Binary Large Objects, any multimedia object within Win OS is called BLOB, not all Databases support BLOBs. the term BLOB was originally used to call the process of moving large amounts of Data from a Database to another Database without any errors.

Logic

How does it work?

Saving BLOBs into a Database is easy as storing anything else. There are 3 famous methods to store files into a Database, these methods are :

  1. Binary
  2. File Pointer
  3. BLOB

We will create a Visual Basic 6.0 project to only store files like Images into Microsoft Access Database 2003 (*.mdb) and retrieve these BLOBS again into the Visual Basic 6.0 Project. We will use ADO 2.8 technology to work with the Database. Storing photos into a Database is the same thing as storing Audio files, Pdf files, Videio files, .... etc.

Using GetChunk()

How to use GetChunk() and AppendChunk() ?

The GetChunk and AppendChunk methods work with the LongVarChar, LongVarWChar, and LongVarBinary column types, also known as TEXT, NTEXT, and IMAGE columns, in Microsoft SQL Server, and as MEMO and OLE fields in Microsoft Jet databases. You can identify these columns in ADO by testing the Type property of a Field for the values adLongVarChar, adLongVarWChar, and adLongVarBinary. You can also test the Attributes property of a Field for the adFldLong flag.

Long columns are commonly referred to as BLOBs (Binary Large OBjects) even though they may contain text data. The sample code below provides two routines, BlobToFile and FileToBlob.

Project Design

What is the project example about

Our Visual Basic 6.0 project example is about creating a friends directory of our friends. We will store Data like [first name, last name & Avatar], the project will contain one form designed like this :

vb6 mdb files saving

As you can see the design is so simple and a source code *.Vbp will be included in the end of the page. It goes like this :

Source Code

Source Code Highlights

OurGeneral Declaration area contains Variables to connect to the Database file MyBase.mdb and Variables to read the Image file :

Option Explicit
'Using ADO2.8
Dim CN As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim Rs_Stream As New ADODB.Stream
Const conChunkSize = 100
Dim Ctrl, Ctrl1 As Control
Dim PicNm, StrTempPic As String
Dim Isize, nHand As Integer
Dim Chunk() As Byte
Dim lngImgSiz, lngOffset As Long
view raw gistfile1.vb hosted with ❤ by GitHub

 Another block of code to read the Image file :

Private Sub ReadPic()
'The Binary Method
Set Rs_Stream = Nothing
StrTempPic = App.Path & "/Temp.JPG"
Rs_Stream.Type = adTypeBinary
Rs_Stream.Open
Rs_Stream.Write RS!PhotoBLOB.Value
'Check the size of the ado stream to -
'make sure there is data
If Rs_Stream.Size > 0 Then
'Write the content of the stream object to a file
'The file will br created if doesn't exists.
'Otherwise over writes the existing file
Rs_Stream.SaveToFile StrTempPic, adSaveCreateOverWrite
'Load the temp Picture into the Image control
Picture1.Picture = LoadPicture(App.Path & "\Temp.JPG")
End If
End Sub
view raw gistfile1.vb hosted with ❤ by GitHub

Another block of code to retrieve the Image file:

Private Sub RetrieveBlob()
'The BLOB Method
StrTempPic = App.Path & "\Temp.jpg"
If Len(Dir(StrTempPic)) > 0 Then
Kill StrTempPic
End If
'Open the temporary file to save the BLOB to
nHand = FreeFile
Open StrTempPic For Binary As #nHand
'Read the binary data into the byte variable array
lngImgSiz = RS("PhotoBLOB").ActualSize
Do While lngOffset < lngImgSiz
Chunk() = RS("PhotoBLOB").GetChunk(conChunkSize)
Put #nHand, , Chunk()
lngOffset = lngOffset + conChunkSize
Loop
Close #nHand
'After loading the image, get rid of the temporary file
Picture1.Picture = LoadPicture(StrTempPic)
Kill StrTempPic
End Sub
view raw gistfile1.vb hosted with ❤ by GitHub

 Downloan the source code from a direct link from Mediafire.com

About Us | Contact Us | ©2013 evry1falls


Free Web Hosting