Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General »

grabar imagenes

Estas en el tema de grabar imagenes en el foro de Bases de Datos General en Foros del Web. necesito saber como se guardan imagenes en sql pero en binario.. se que existen dos tipos de datos image que puede guardar hasta 8 megas.. ...
  #1 (permalink)  
Antiguo 31/07/2003, 14:51
 
Fecha de Ingreso: febrero-2002
Mensajes: 157
Antigüedad: 22 años, 2 meses
Puntos: 0
grabar imagenes

necesito saber como se guardan imagenes en sql pero en binario.. se que existen dos
tipos de datos
image que puede guardar hasta 8 megas..
y otra que es binario pero no se como guardarlo o transformarlo y despues
leerlo...


alguien que me pueda ayudar porfavor ....


de antemano gracias.
  #2 (permalink)  
Antiguo 31/07/2003, 19:53
 
Fecha de Ingreso: julio-2003
Mensajes: 120
Antigüedad: 20 años, 9 meses
Puntos: 0
Nosotros tuvimos el mismo problema. No me encargué yo de solucionarlo y no te puedo dar demasiados detalles, pero por si te sirve de algo te mando un artículo en q se basó "nuestra" solución (q fue, sobre el código del artículo, hacer un programita que hiciera las inserciones). El problema es que el ejemplo que sigue vale sólo para Visual Basic, no sé si te será útil (aunq el ejemplo sea sobre Access, tb funciona, por lo menos, para los campos "image" de SQLServer 2000)



Storing Images In A Database
Author: Kaustav Neogy
Rating:
Visits: 1864

Discuss in Newsgroups



We often require storing image data in the database along with other information. Images consume more space as compared to other textual or numeric data. Depending on the image format, the database size may vary greatly thereby affecting the overall performance of the application.

Storing of images in a database is a very delicate issue. In Microsoft Access, we can store images in the database as OLE Objects. However this increases the size of the database to quite an extent, which isn't a good practice. RDBMS's like Oracle provides for LOB's (Large Objects) and BFILES (Binary Files) to tackle this issue. However, in the case of LOB'S storing and retrieving of a LOB column is a bit complicated and BFILES too is no exception. It requires that a logical directory be created in Oracle, the files be stored in a directory with a similar name and many more considerations.

One easy way out to this issue could be to just store the path of the images in the Database and then retrieve them when needed. This would have the following advantages-
· Database size would be highly reduced
· Performance would improve
· Editing of image data would be easier as it is kept separate outside
the database


Visual Basic 6.0 has the Data Report as a reporting tool. However the Data Report isn't without limitations regarding displaying of images from a database. A picture field is dropped as a textbox on a Data Report. When we insert a Picture field on a Data Report, it appears as a text box instead of an image box. When the Report is run, the picture field is displayed as a special character. Same is the situation with Binary Large Object (BLOB) fields.

Microsoft has confirmed this to be a bug with the Data Reports in Microsoft Visual Basic Enterprise Edition for Windows, version 6.0. The details of this bug can be found here

http://support.microsoft.com/default...;EN-US;q196588

Of course one has the option to use third party reporting tools like Seagate's Crystal Reports to resolve this problem.

The following example shows how to store and retrieve image data using MS Access 2000 and Visual Basic 6.0. and also display the image in a Data Report. Here I select the image using the CommonDialog Control and store its path in the database. While retrieving, I read the path stored in the database and then use the LoadPicture function to display the image. If there is no image to be inserted, I substitute the path with "0" to indicate that there is no image on record.

I have created a database demo.mdb which has a table named demo with the following fields:

Field Type

Name Text
Photo Text

Next I've created a new Standard Exe Project in VB 6.0. I added a Data Report to it. I then added the following controls onto Form1-

TextBoxes - txtname, txtphoto (stores the path of the image).
CommonDialog - cdl1.
Adodc - adodc1.
Image - image1.
CommandButtons - cmdfirst, cmdprevious, cmdnext, cmdlast, cmdadd, cmddelete, cmdupdate, cmdreport.

On the Data Report, I added an image control - image1.

For adodc1 in Form1, assign the following properties
· Connection String
· Record source

I had set the visible property of txtphoto to false as it would be used to store the image path only. Also set the visible property of adodc1 to false.

Following is the code for the whole project -

'Declarations

Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim path As String

Public Sub view()
path = txtphoto.Text
If path = "0" Then
Image1.Visible = False
Else
Image1.Picture = LoadPicture(path)
Image1.Visible = True
End If
End Sub


Private Sub cmdadd_Click()
Adodc1.Recordset.AddNew
If (MsgBox("Insert Photo", vbYesNo + vbQuestion, "Insert Photo") = vbYes) Then
cdl1.ShowOpen
If cdl1.FileName = Empty Then
txtphoto.Text = "0"
Else
txtphoto.Text = cdl1.FileName
view
End If
Else
txtphoto.Text = "0"
End If
txtname.SetFocus
cmddelete.Enabled = True
cmdupdate.Enabled = True
cmdreport.Enabled = True
End Sub


Private Sub cmddelete_Click()
If Adodc1.Recordset.RecordCount = 0 Then
Exit Sub
End If
Adodc1.Recordset.Delete 'adAffectCurrent
Adodc1.Recordset.MovePrevious
view
End Sub


Private Sub cmdreport_Click()
Dim dbpath, img, strSQL As String
dbpath = App.path & "\demo.mdb"
If conn.State = adStateOpen Then
conn.Close
End If
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & ";Persist Security Info=False"
strSQL = "select photo from demo where name='" & txtname.Text & "'"
conn.Open
rs.Open strSQL, conn, adOpenDynamic, adLockOptimistic
Set DataReport1.DataSource = rs
img = rs.Fields(0)
If img = "0" Then
MsgBox "No Image On Record"
Exit Sub
Else
Set DataReport1.Sections(2).Controls("Image1").Picture = LoadPicture(img)
DataReport1.Show
End If
End Sub


Private Sub cmdupdate_Click()
If (MsgBox("Update Photo", vbYesNo + vbQuestion, "Update Photo") = vbYes) Then
cdl1.ShowOpen
If cdl1.FileName = Empty Then
txtphoto.Text = "0"
Else
txtphoto.Text = cdl1.FileName
view
End If
End If
Adodc1.Recordset.Update
view
End Sub


Private Sub Form_Load()
If Adodc1.Recordset.RecordCount = 0 Then
cmddelete.Enabled = False
cmdupdate.Enabled = False
cmdreport.Enabled = False
Else
cmddelete.Enabled = True
cmdupdate.Enabled = True
cmdreport.Enabled = True
End If
cdl1.InitDir = App.path & "\images"
' I've assumed that images are stored in the folder named images
view
End Sub


Private Sub cmdfirst_Click()
Adodc1.Recordset.MoveFirst
view
End Sub


Private Sub cmdlast_Click()
Adodc1.Recordset.MoveLast
view
End Sub


Private Sub cmdnext_Click()
If Adodc1.Recordset.EOF Then
Exit Sub
End If
If Adodc1.Recordset.AbsolutePosition = Adodc1.Recordset.RecordCount Then
cmdnext.Enabled = False
Else
Adodc1.Recordset.MoveNext
view
End If
cmdnext.Enabled = True
End Sub


Private Sub cmdprevious_Click()
If Adodc1.Recordset.BOF Then
Exit Sub
End If
If Adodc1.Recordset.AbsolutePosition = 1 Then
cmdprevious.Enabled = False
Else
Adodc1.Recordset.MovePrevious
view
End If
cmdprevious.Enabled = True
End Sub
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:28.