![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
13/03/2008, 10:49
|
![Avatar de Peterpay](http://static.forosdelweb.com/customavatars/avatar194134_3.gif) | Colaborador | | Fecha de Ingreso: septiembre-2007 Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 17 años, 5 meses Puntos: 87 | |
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";
}
} |