如何使用C#从SQL数据库检索图像?如何使用C#从SQL数据库检索图像?
1
A
回答
2
0
为什么不尝试使用查询字符串。这里是我的代码,在查询字符串中使用,我从数据库中获取图像,而不必在您的本地文件夹中创建任何想要的图像的instatnce。跳这有助于。
<%@ WebHandler Language="C#" Class="DisplayImg" %>
using System;
using System.Web;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class DisplayImg : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string theID;
if (context.Request.QueryString["id"] != null)
theID = context.Request.QueryString["id"].ToString();
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = DisplayImage(theID);
byte[] buffer = new byte[2048];
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
public Stream DisplayImage(string theID)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString.ToString());
string sql = "SELECT Server_image_icon FROM tbl_ServerMaster WHERE server_Code = @ID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", theID);
connection.Open();
object theImg = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])theImg);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
只需添加一行在CS代码
UploadImg.ImageUrl = “〜/ DisplayImg.ashx ID =?” +代码;
+0
-1不使用'using',不检查空值,并返回方法中的'stream'。 – avishayp 2012-11-16 07:31:07
相关问题
- 1. 如何从数据库使用数据库检索图像?
- 2. 从sql数据库检索图像
- 3. 如何从数据库检索图像?
- 4. 如何使用C#在ASP.NET中从SQL Server数据库检索图像?
- 5. 如何使用spring从Mysql数据库中检索图像?
- 6. 如何从C#插入图像并从数据库中检索图像?
- 7. 从c#中的数据库检索图像到数据网格#
- 8. 从Oracle数据库检索图像
- 9. 从数据库中检索图像
- 10. 从数据库检索图像ASP.NET MVC
- 11. 如何使用函数从Web SQL数据库中检索值?
- 12. 从c#winforms中的数据库检索图像到图片框
- 13. 如何使用PHP在数据库中存在多个图像时从数据库检索单个图像?
- 14. 从Linq到SQL的数据库检索图像
- 15. 从sql数据库检索图像,并将其张贴在表
- 16. javafx从sql server数据库检索图像
- 17. 从sql数据库检索图像的路径
- 18. 从sql数据库检索图像(字节数组)并显示图像
- 19. 使用c保存和检索SQL Server数据库中的图像
- 20. 如何检索图像从数据库到c中的图片框#
- 21. 使用cideigniter从数据库检索视频和背景图像
- 22. 使用java从MYSQL数据库检索多个图像
- 23. 使用Python存储和从数据库检索图像
- 24. 使用NHibernate从数据库中检索图像字段UniqueResult()
- 25. 使用Python从MySQL数据库中检索多个图像3
- 26. 使用PHP从postgres数据库检索图像
- 27. 使用PHP显示从数据库检索的图像
- 28. 使用Ajax从数据库检索图像
- 29. 如何从sql server检索图像
- 30. 如何从数据库检索图像并放置在JSP上?
Google“Display Blob Image C#” – 2011-03-31 09:47:34
您的方法是什么?为什么它不工作(异常消息,编译器错误...)? – 2011-03-31 09:48:15
继续吧,先试试吧,如果你被困在某个地方,就会复出! – 2011-03-31 09:51:57