日期:2014-05-18  浏览次数:20448 次

怎样实现图片存取数据库里?
我想在asp.net 下实现 对图片存入sqlserver2005 里

数据库test
表 testimage
字段 id identity
  imagedata image

要怎么的方式实现最简单? 先浏览图片,然后上传到数据库,然后显示出来!

------解决方案--------------------
放个fileupload在一个页面上
保持图片到数据库里:
int intImageSize;
string strImageType;
Stream ImageStream;

intImageSize = this.Image_Fup.PostedFile.ContentLength;
ImageStream = this.Image_Fup.PostedFile.InputStream;
strImageType = this.Image_Fup.PostedFile.ContentType;
Byte[] ImageContent = new Byte[intImageSize];
int intStatus;
intStatus = ImageStream.Read(ImageContent, 0, intImageSize);

sat.Images = ImageContent;
//sat.Images = ConvertImage.PhotoToArray(ImgPath);
//将图片上传到服务器上

if (ImgPath != "")
{
Image_Fup.PostedFile.SaveAs(path + Image_Fup.FileName);


在放个image控件连接到你放图片的另外一个网页

从数据库中读出图片数据
MemoryStream stream = new MemoryStream();
SqlConnection conn = DataAccess.CreateConn();
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
byte[] image = (byte[])cmd.ExecuteScalar();
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
Response.ContentType = "image/jpg";
Response.ContentType = "image/gif";
Response.ContentType = "image/bmp";
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
bitmap.Save(Response.OutputStream, ImageFormat.Bmp);
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
conn.Close();
stream.Close();
}