Вы находитесь на странице: 1из 3

How to insert a JPG image into an Oracle Database

5/24/2007
By ITtoolbox Popular Q&A Team for ITtoolbox as adapted from VisualBasic-L
discussion group

Summary:
I would like to know how to insert .jpg images from a directory on the hard disk
into an Oracle database. The images to be inserted must be done so with other data
in an Excel sheet. Preferrably, the Excel sheet would have 3 columns of data, with
one of the fields pointing to where the image is stored.
Full Article:
Disclaimer: Contents are not reviewed for correctness and are not endorsed or
recommended by ITtoolbox or any vendor. Popular Q&A contents include summarized
information from VisualBasic-L discussion unless otherwise noted.

Adapted from a response by VeenaMG on 5/24/2007

In the Oracle table create 3 fields:

ImgID (Number (10,0))


ImgData (Long Raw)
FileLen (Number (10,0))

In Form Load :
adoCn -> Connection Open
and give iData =1 (change it to any value Later)

Declare this Stuff in Form Level :

Dim strSQL As String


Dim i As Integer
Const BlockSize = 100000
Dim iData As Integer

Private Sub cmdSave_Click()


'
Dim PictBmp As String
Dim ByteData() As Byte
Dim SourceFile As Long
Dim RS As New ADODB.Recordset
'
Dim FileLength As Long
Dim Numblocks As Integer
Dim LeftOver As Long
'
strSQL = "Select * from AImage WHERE imgid=" & iData
Set RS = Nothing
RS.CursorLocation = adUseClient
RS.Open strSQL, adoCn, adOpenKeyset, adLockOptimistic
'
PictBmp = "C:\c1.jpg"
'
SourceFile = FreeFile
Open PictBmp For Binary Access Read As SourceFile
FileLength = LOF(SourceFile)
'
If FileLength = 0 Then
Close SourceFile
MsgBox PictBmp & " empty or not found."
Exit Sub
Else
Numblocks = FileLength / BlockSize
LeftOver = FileLength Mod BlockSize
ReDim ByteData(LeftOver)
Get SourceFile, , ByteData()
If RS.EOF Then
RS.AddNew
RS(0) = iData
End If
RS(1).AppendChunk ByteData()
ReDim ByteData(BlockSize)
For i = 1 To Numblocks
Get SourceFile, , ByteData()
RS(1).AppendChunk ByteData()
Next i
RS(2) = FileLength
RS.Update
Close SourceFile
End If
Me.MousePointer = vbNormal
MsgBox "saved"
End Sub

To Retrieve In a Picture Box :

Private Sub cmdRetreive_Click()

Dim ByteData() As Byte


Dim DestFileNum As Integer
Dim DiskFile As String
Dim RS As New ADODB.Recordset
Dim FileLength As Long
Dim Numblocks As Integer
Dim LeftOver As Long

Set RS = Nothing
'
strSQL = "Select * from AImage WHERE IMGID=" & iData
Set RS = Nothing
RS.CursorLocation = adUseClient
RS.Open strSQL, adoCn, adOpenKeyset, adLockOptimistic
'
If RS.EOF Then Exit Sub
RS.MoveFirst
'
FileLength = RS("FileLen")
'
Numblocks = FileLength / BlockSize
LeftOver = FileLength Mod BlockSize
'
Me.MousePointer = vbHourglass
'
DiskFile = App.Path & "\image1.jpg"
If Len(Dir$(DiskFile)) > 0 Then
Kill DiskFile
End If
'
DestFileNum = FreeFile
Open DiskFile For Binary As DestFileNum
'
Numblocks = FileLength / BlockSize
LeftOver = FileLength Mod BlockSize
'
ByteData() = RS(1).GetChunk(LeftOver)
Put DestFileNum, , ByteData()
'
For i = 1 To Numblocks
ByteData() = RS(1).GetChunk(BlockSize)
Put DestFileNum, , ByteData()
Next i
'
Close DestFileNum
'
Picture1.Visible = True
Picture1.Picture = LoadPicture(App.Path & "\image1.jpg")
RS.Close
Me.MousePointer = vbNormal
End Sub

Вам также может понравиться