Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   .NET (http://www.forosdelweb.com/f29/)
-   -   Guardar archivo en BD (http://www.forosdelweb.com/f29/guardar-archivo-bd-565712/)

seques 13/03/2008 09:28

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

Peterpay 13/03/2008 10:49

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";
}
}


La zona horaria es GMT -6. Ahora son las 01:45.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.