Retroceder   Foros del Web > Programación para sitios web > .NET > win forms

Respuesta
 
Herramientas Desplegado
Antiguo 13-mar-2008, 09:28   #1 (permalink)
seques ha deshabilitado el karma
 
Fecha de Ingreso: mayo-2006
Mensajes: 109
Guardar archivo en BD

Buen dia

Alguien me puede ayudar con una rutina para guardar un archivo (especificamente una imagen) en una base de datos sql server y luego de guardado hacer una consulta para obtener su nombre...

Gracias
seques está desconectado   Responder Citando
Antiguo 13-mar-2008, 10:49   #2 (permalink)
Peterpay está en el buen camino
 
Avatar de Peterpay
 
Fecha de Ingreso: septiembre-2007
Ubicación: Mexico
Mensajes: 1.799
Enviar un mensaje por MSN a Peterpay Enviar un mensaje por Skype™ a Peterpay
Re: Guardar archivo en BD

Guardando imagen en BD (SQL Server)

SqlCommand com = new SqlCommand("insert into StudentPhoto values(@roll,@photo)", con);
com.Parameters.Add(new SqlParameter("@roll", SqlDbType.Int));
com.Parameters.Add(new SqlParameter("@photo", SqlDbType.Image));

int imgSize = FilePhoto.PostedFile.ContentLength;
Stream imgStream = FilePhoto.PostedFile.InputStream;
byte[] imgContent = new byte[imgSize];
imgStream.Read(imgContent, 0, imgSize);

com.Parameters["@roll"].Value = Int32.Parse(TxtRoll.Text);
com.Parameters["@photo"].Value = imgContent;

con.Open();
com.ExecuteNonQuery();
con.Close();

Leer Imagen

SqlCommand com = new SqlCommand("select photo from studentphoto where roll=" + Int32.Parse(ComboRoll.SelectedItem.Text), con);
con.Open();
SqlDataReader rd=com.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
{
System.Data.SqlTypes.SqlBytes photo=rd.GetSqlBytes(0);
Bitmap bmp = new Bitmap(photo.Stream);
FileStream fs=new FileStream("c:\\abc.jpg",FileMode.Create);
bmp.Save(fs,System.Drawing.Imaging.ImageFormat.Jpe g);
StudImage.ImageUrl = "c:\\abc.jpg";
}
}
__________________
Saludos
Peterpay
MCPD Enterprise Applications
Peterpay está desconectado   Responder Citando
Respuesta

No hay votos aún.


Herramientas
Desplegado

Normas de Publicación
No puedes crear nuevos temas
No puedes responder temas
No puedes subir archivos adjuntos
No puedes editar tus mensajes

BB code is Activado
Caritas están Activado
[IMG] está Activado
Código HTML está Desactivado


La Zona horaria es GMT -6. Ahora son las 21:20.


Message Board Statistics

LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93