从sql server数据库中检索图像

我正在将图像存储到数据库中。 如何从数据库中检索所有图像。

例如:从imagetable中选择图像

问题:

数据逻辑:

while (dr.Read()) { ///Check for null if (!dr.IsDBNull(0)) { try { ///Converting the image into bitmap byte[] photo = (byte[])dr[0]; ms = new MemoryStream(photo); Bitmap bm = new Bitmap(ms); bmp[i] = bm; ms.Close(); } catch (Exception ex) { } } 

ASpx.CS页面:

 Bitmap[] bm= photos.GetImage(); for (int i = 0; i < bm.Length; i++) { MemoryStream ms = new MemoryStream(); **bm[i].Save(ms, ImageFormat.Jpeg);**(Error : A generic error occurred in GDI+.) htmlCode.Append("
  • "); }

    图像无法显示

    格塔

    这是来自Sql Server的一个例子

      connection.Open(); SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@param", connection); SqlParameter myparam = command1.Parameters.Add("@param", SqlDbType.NVarChar, 30); myparam.Value = txtimgname.Text; byte[] img = (byte[])command1.ExecuteScalar(); MemoryStream str = new MemoryStream(); str.Write(img, 0, img.Length); Bitmap bit = new Bitmap(str); connection.Close(); 

    请看http://www.akadia.com/services/dotnet_read_write_blob.html

    对于SQL Server 2008以上,FILESTREAM几乎肯定是要走的路。

    请参阅: SQL Server 2005 – 如何将图像数据类型转换为字符格式

    您需要从数据库中获取二进制数据,然后将二进制数据作为映像流式传输到浏览器。

    您将图像的Url设置为字节流 – 您需要将图像保存到硬盘驱动器并提供位置。

    一种更好的方法是将图像URL设置为具有参数的资源处理程序,然后可以从数据库中检索图像并将其作为流返回给浏览器。