Os dejo un ejemplo que se comentó en las listas de gambas y en el foro de gambas por Shordi
Ejemplo original que apareció en las listas de gambas ( ) : enlace descarga 0.0.1
Este ejemplo lo he modificado un poco para que se use el evento Data del gridviews para rellenar los datos.
enlace de descarga ejemplo modificado: enlace de descarga modificado version 0.0.2
Os dejo aqui las partes más interesante del codigo fuente:
'–---------------------------------------------
'Para crear la base de datos con código
'–---------------------------------------------
Public
Sub
btnCreate_Click()
Dim
hTable As
Table
If
Dialog.SaveFile()
Then
Return
Try
$hConn.Close()
$hConn.Host
=
File.Dir(Dialog.Path)
$hConn.Name
=
File.Name(Dialog.Path)
Try
$hConn.Open()
'
Database missing? Create it.
If
Error
Then
$hConn.Name
=
""
$hConn.Open()
$hConn.Databases.Add(File.Name(Dialog.Path))
$hConn.Close()
$hConn.Name
=
File.Name(Dialog.Path)
$hConn.Open()
Endif
'
Table missing? Create it. It is named "blobs".
If
Not
$hConn.Tables.Exist("blobs")
Then
hTable =
$hConn.Tables.Add("blobs")
'
File name and (binary) data
hTable.Fields.Add("name",
DB.String)
hTable.Fields.Add("data",
DB.Blob)
hTable.PrimaryKey
=
["name"]
hTable.Update()
Endif
txtSelect.Text
=
Dialog.Path
btnImport.Enabled
=
True
btnExport.Enabled
=
True
UpdateDisplay()
End
'–---------------------------------------------
'Para mostrar la información en el
gridview
'–---------------------------------------------
Private
Sub
UpdateDisplay()
Dim
iInd As
Integer
hRes =
$hConn.Find("blobs")
gvwBlobs.Rows.Count
=
0
gvwBlobs.Rows.Count
=
hRes.Count
'
iInd = 0
'
For Each hRes
'
gvwBlobs[iInd, 0].Text = hRes["name"]
'
hBlob = hRes["data"]
'
' We don't show the entire binary blob but only the first 16
'
' bytes base64 encoded - just for aesthetic reasons.
'
gvwBlobs[iInd, 1].Text = Base64$(Left$(hBlob.Data, 16))
'
Inc iInd
'Next
End
Public
Sub
gvwBlobs_Data(row As
Integer,
column As
Integer)
Dim
hBlob As
Blob
'
este evento se encarga de dibujar los datos en el gridviews, para que
aparezcan en la pantalla.
Try
hRes.MoveTo(row)
If
Error
Then
Return
If
hRes.Available
Then
If
column =
0
Then
gvwBlobs.Data.text
=
hRes["name"]
Else
'
We don't show the entire binary blob but only the first 16
'
bytes base64 encoded - just for aesthetic reasons.
hBlob =
hRes["data"]
gvwBlobs.Data.text
=
Base64$(Left$(hblob.data,
16))
Endif
If
gvwBlobs.row
=
-1
Then
gvwBlobs.row
=
0
Endif
If
row =
gvwBlobs.row
Then
'cursor
donde esta situado en el gridview
gvwBlobs.Data.Background
=
Color.Orange
Else
If
row Mod
2
=
0
Then
gvwBlobs.Data.Background
=
Color.Cyan
Endif
Endif
End
'–---------------------------------------------
'Para añadir un archivo a la base de
datos (importar)
'–---------------------------------------------
Public
Sub
btnImport_Click()
Dim
hNew As
Result
'
Make the user select a file to import
If
Dialog.OpenFile()
Then
Return
hNew =
$hConn.Create("blobs")
'
Assign the file basename to the "name" field in the
database
hNew["name"]
=
File.Name(Dialog.Path)
'
Read the file into the blob field
hNew["data"]
=
File.Load(Dialog.Path)
'
Write to the database
hNew.Update()
UpdateDisplay()
End
'–---------------------------------------------
'Para extraer un archivo a la base de
datos (exportar)
'–---------------------------------------------
Public
Sub
btnExport_Click()
Dim
sName As
String
Dim
hres2 As
Result
Dim
hBlob As
Blob
'
Make the user select a file to save the blob to
Dialog.Path
=
gvwBlobs[gvwBlobs.Row,
0].Text
If
Dialog.SaveFile()
Then
Return
'
Get the record the user clicked at. We use the "name" field
which is the
'
primary key to search the database table
sName =
gvwBlobs[gvwBlobs.Row,
0].Text
hRes2 =
$hConn.Find("blobs",
"name=&1",
sName)
If
Not
hRes2.Available
Or If
Not
hRes2.Count
Then
Message.Error(("Something
went wrong..."))
Return
Endif
'
Get the blob
hBlob =
hRes2["data"]
'
A blob is just a string in Gambas. So we just have to write it where
'
the user wants it to be written to.
File.Save(Dialog.Path,
hBlob.Data)
Message.Info(("OK"))
UpdateDisplay()
End
Fuente:
Shordi: http://www.gambas-es.org/viewtopic.php?f=5&t=3355
No hay comentarios:
Publicar un comentario