Picacodigos
« DataGrid de sólo lectura en Windows Form...
|
Mas Reciente
|
Bug en Peeker »
Introducir y recuperar imágenes en Microsoft Access
Published 131 weeks, 1 day ago
Mon
Apr
03
2006
Si hace unos días hablábamos de cómo
guardar ficheros binarios en SQL Server
, hoy le toca a Microsoft Access. El proceso es similar, aunque para este ejemplo he decidido usar imágenes como los datos binarios que vamos a guardar y recuperar de la base de datos.
Imaginemos que tenemos una sencilla base de datos Access, con una única tabla llamada
Images
, que tiene la siguiente estructura:
Nombre
Tipo
imgID
int (Clave autonumérica)
FileName
Texto(255)
ImageData
Objeto OLE
El campo
imgID
almacenará una clave única autonumérica para identificar a cada registro.
FileName
contendrá el nombre del fichero de imagen, e
ImageData
contendrá
la imagen
propiamente dicha, en formato binario.
El procedimiento para insertar imágenes en la base de datos Access es como sigue:
private void
SaveData(
string
fileName)
{
try
{
//Creamos un nuevo FileStream a partir del fichero parámetro
//
//We create a new FileStream from the file specified as parameter
FileStream fs
= new
FileStream(fileName, FileMode.Open)
;
//Declaramos un array de bytes del tamaño del FileStream
//
//We declare a new byte array as big as the FileStream
Byte[] data
= new byte
[fs.Length]
;
//E introducimos los datos del FileStream en el array
//
//And we enter the data from the FileStream into the array
fs.Read(data,
0
, Convert.ToInt32(fs.Length))
;
fs.Close()
;
//Creamos un nuevo DataAdapter, DataSet, CommandBuilder and DataRow
//para insertar el nuevo registro
//
//We create a new DataAdapter, DataSet, CommandBuilder and DataRow
//to perform the insertion of the new record
if
(conn.State !
=
ConnectionState.Open ) conn.Open()
;
OleDbDataAdapter adap
=
new
OleDbDataAdapter(
"SELECT * FROM Images"
, conn)
;
adap.MissingSchemaAction
=
MissingSchemaAction.AddWithKey
;
OleDbCommandBuilder cmdBuilder
= new
OleDbCommandBuilder(adap)
;
adap.InsertCommand
=
cmdBuilder.GetInsertCommand()
;
DataSet ds
= new
DataSet()
;
adap.Fill(ds)
;
DataRow dr
=
ds.Tables[
0
].NewRow()
;
dr[
"FileName"
]
=
fileName
;
dr[
"ImageData"
]
=
data
;
//The byte array
ds.Tables[
0
].Rows.Add(dr)
;
adap.Update(ds)
;
conn.Close()
;
//Esta funcion refresca un ListBox para que se refleje
//el nuevo registro insertado
//
//This function refreshes a ListBox to show the newly inserted record
LoadData()
;
MessageBox.Show(
"Done!\nImage file: "
+ fileName +
" added to the database."
)
;
}
catch
(Exception e)
{
MessageBox.Show(e.Message)
;
}
}
Colorized by:
CarlosAg.CodeColorizer
Y los procedimientos que extrae las imágenes de la base de datos es tal que así:
/// <summary>
/// Coloca en el control Image la imagen recuperada de base de
/// datos por la función LoadImage, en base al índice único de
/// la imagen en la base de datos.
///
/// Sets in the Image control an image recovered from the database
/// via the LoadImage function, thanks to the unique identifier
/// of the image on the database.
/// </summary>
/// <param name="imgIndex">El identificador único de la imagen
///en la base de datos</param>
/// <param name="imgIndex">The unique identifier of the image
///on the database</param>
private void
LoadImageControl(
string
imgIndex)
{
try
{
//Creamos un nuevo stream de memoria con el resultado
//de la función LoadImage, que devuelve un array de bytes
//
//We create a new MemoryStream with a byte array,
//returned by the LoadImage function
MemoryStream mem
= new
MemoryStream(LoadImage(imgIndex))
;
//Y establecemos que la imagen del control picImage es una nueva imagen,
//a partir del stream en memoria mem
//
//And we set the picImage control's image is a new image,
//built from the memory stream mem
picImage.Image
=
Image.FromStream(mem)
;
}
catch
(Exception e)
{
MessageBox.Show(e.Message)
;
}
}
/// <summary>
/// Carga la imagen de la base de datos en un array
//de bytes y devuelve dicho array
///
/// Loads the image from the database to a byte array and returns that array
/// </summary>
/// <param name="imgIndex">El ID unico de la imagen</param>
/// <param name="imgIndex">The unique ID of the image</param>
/// <returns>Devuelve la imagen en un array de bytes</returns>
/// <returns>The image on a byte array</returns>
private byte
[] LoadImage(
string
imgIndex)
{
if
(conn.State !
=
ConnectionState.Open ) conn.Open()
;
sSQL
=
"SELECT ImageData FROM Images WHERE imgID = "
+ imgIndex
;
OleDbCommand cmd
= new
OleDbCommand(sSQL, conn)
;
byte
[] imgData
=
(
byte
[]) cmd.ExecuteScalar()
;
conn.Close()
;
return
imgData
;
}
Colorized by:
CarlosAg.CodeColorizer
En cualquier caso, tenéis disponible
aquí
el código fuente completo, con comentarios en inglés y en español para esta aplicación. Descargadlo y jugad un poco con él.
NOTA
.- Más adelante publicaré la forma de compactar y reparar mediante código C# una base de datos Microsoft Access. Aplicaciones como el código fuente que tenéis para descargar lo necesitan, y mucho.
Categorías:
.NET
|
Artículos
|
Comments [0]
Comments are closed.
Buscar
Estadísticas
Total Posts: 112
This Year: 3
This Month: 0
This Week: 0
Comments: 328
Comentarios Recientes
RE: Microsoft Techdays y...
by Erick Alejan...
RE: Microsoft Techdays y...
by Venkman
RE: Microsoft Techdays y...
by Jesús
RE: Microsoft Techdays y...
by espinete
RE: Microsoft Techdays y...
by Pavleras
Software
Peeker
Acerca de...
Curriculum
Resume
Suscripcion
Suscribirse por correo
Categorias
.NET
Artículos
Blog
Eventos
Gadgets
Hardware
Herramientas
Opinión
Personal
ReSharper
Trucos
Ubuntu
Web
Archivo
January, 2008 (3)
December, 2007 (7)
November, 2007 (13)
October, 2007 (6)
September, 2007 (10)
August, 2007 (1)
July, 2007 (2)
June, 2007 (4)
May, 2007 (3)
March, 2007 (3)
February, 2007 (5)
January, 2007 (5)
December, 2006 (3)
November, 2006 (5)
September, 2006 (2)
August, 2006 (4)
July, 2006 (7)
June, 2006 (3)
May, 2006 (4)
April, 2006 (5)
March, 2006 (5)
February, 2006 (4)
January, 2006 (3)
Los blogs que leo
.Avery Blog
47 hats
Coding Horror
El lado del mal
Haacked
Jon Galloway
NET slave
Rob Conery
secretGeek
The daily WTF
The old new thing
Thinking in .NET
Velocidad de Escape
Contacto
Microblogging
Login
Sign In
Herramientas
Addicted to
The best C# & VB.NET refactoring plugin for Visual Studio 2005
Powered by
newtelligence dasBlog 1.9.7174.0
Tema basado en
Dandelion
, de Tim Sherrill