|
-
Jul 9th, 2002, 03:30 AM
#1
Thread Starter
New Member
Inserting a picture into a database field
Hi everyone,
I'm new to this forum, so hopefully it helps me out.
I'm looking for someone to help me out with a problem I have trying to insert a picture into a database field.
The user selects a file via a file selector, typically a JPG, BMP, GIF, etc. I then need to be able to insert that file programmatically into a database table / field.
Asumming the table name is "TABLE", field name is "FIELD" and the file name the user selected is "IMAGE.JPG", could someone please show me some code that would allow me to insert the IMAGE.JPG file into the TABLE.FIELD field.
Thanks in advance,
Warren.
-
Jul 9th, 2002, 03:43 AM
#2
-
Jul 9th, 2002, 03:55 AM
#3
New Member
Hi
You might have known that there TWO methods of recordset type object,eg, GetChunk() and AppendChunk(). These two methods will give you the ability to store images in the database and retriving that.
Open the Image File in Binary mode. Read the File data do a Byte type of BufferArray. Store that data in the Database using the AppendChunk() Method.
Similary while retriving the image data open an Temp Binary file. Retrive the data from the Table Filed using GetChunk() Method and write that data to the File.
If any other problem plz feel free to mail me.
Mr Utkal Ranjan(http://utkalbuzz.tripod.com)
=======================================
I'm storing the data in the click event of a Browse button
Private Sub cmdBrowse_Click()
On Error GoTo Message
' Check for null Image Title
If Me.txtTitle.Text = "" Then
MsgBox "Please give a suitable title for your image", vbInformation + vbOKOnly, "Image Title"
SendKeys "{Home}+{End}"
Me.txtTitle.SetFocus
Exit Sub
End If
'Browse for the Source file
CD.Filter = "All Graphics Files(*.bmp,*.jpg,*.jpeg)|*.bmp;*.jpg;*.jpeg"
CD.ShowOpen
FilePath = CD.FileName
FileName = CD.FileTitle
Me.txtSource = FilePath
' Check for Null File name
If FilePath = "" Then
Me.txtTitle.Text = ""
Call Disable_Controls
Me.cmdAdd.SetFocus
Exit Sub
End If
' Store the Image in Database
Dim nFile As Integer
Dim ByteData() As Byte
Dim i As Integer
'Set rs = New Recordset
'rs.Open "Select * from image", db, adOpenStatic, adLockOptimistic
rs.AddNew
Me.MousePointer = vbHourglass
Me.Caption = "Storing the Image ..."
nFile = FreeFile
Open FilePath For Binary Access Read As nFile
FileLength = LOF(nFile)
If FileLength = 0 Then
Close nFile
MsgBox FilePath & " empty or not found."
Exit Sub
Else
No_Of_Blocks = FileLength / Block_Size
LeftOver = FileLength Mod Block_Size
ReDim ByteData(LeftOver)
Get nFile, , ByteData()
rs(4).AppendChunk ByteData()
ReDim ByteData(Block_Size)
For i = 1 To No_Of_Blocks
Get nFile, , ByteData()
rs(4).AppendChunk ByteData()
Next i
rs(1) = Me.txtTitle.Text
rs(2) = FileName
rs(3) = FileLength
rs.Update
Close nFile
End If
Me.Caption = "Image Database"
Me.MousePointer = vbNormal
Me.img.Picture = LoadPicture(FilePath)
Call Disable_Controls
' Now Enable the Data Navigation Controls to enable State
Me.cmdDelete.Enabled = True
If rs.RecordCount > 1 Then
Me.cmdFirst.Enabled = True
Me.cmdLast.Enabled = True
Me.cmdNext.Enabled = True
Me.cmdPrevious.Enabled = True
End If
Call ShowRecordNo
Exit Sub
Message:
MsgBox Err.Description, vbInformation + vbOKOnly, "Error ..."
End Sub
Private Sub cmdDelete_Click()
On Error GoTo Message
' Delete the Current Record From the Database
If MsgBox("Are you sure to delete the record ?", vbQuestion + vbYesNo, "Confirmation ...") = vbYes Then
' Delete the record then move to previous record
rs.Delete
If rs.RecordCount = 1 Then
rs.MoveFirst
Me.cmdFirst.Enabled = False
Me.cmdLast.Enabled = False
Me.cmdNext.Enabled = False
Me.cmdPrevious.Enabled = False
ElseIf rs.RecordCount = 0 Then
Me.cmdDelete.Enabled = False
Else
rs.MovePrevious
End If
Call Fill_Controls
End If
Exit Sub
Message:
MsgBox Err.Description, vbInformation + vbOKOnly, "Error ..."
End Sub
-
Jul 9th, 2002, 04:22 AM
#4
Fanatic Member
-
Jul 9th, 2002, 04:38 AM
#5
Originally posted by faisalkm
but did he say what kind of database he is using? or this code works for all dbs..wellli don't think so
it should work with sql and oracle dont thing it will work with access
-
Jul 9th, 2002, 04:49 AM
#6
Fanatic Member
-
Jul 9th, 2002, 04:50 AM
#7
Originally posted by faisalkm
well it works with Access. I have tried that once
thanks for info...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|