我数据库中有个image类型的字段,里面存的是图片。现在我想将它取出来显示在页面上该如何做?
小白哈,求高手代码能稍微明确点儿谢了哈。
小白哈,求高手代码能稍微明确点儿谢了哈。
解决方案 »
- asp.net 控件列表怎么处理一对多情况(关于行列互换问题)
- 使用asp.net给word添加水印
- 怎样批量删除一些表中的数据?
- vs2008 asp.net(c#)中 列变行的问题
- asp.net使用OLEDB将Excel导入Sqlserver的问题
- DropDownList 样式问题
- 如何用在cs代码中弹出_blank类型的窗口
- 郁闷,再发TabStrip and MultiPage的问题
- 如何设置Datagrid的两个主键字段,因为一个表有两个主键,然后怎样从中提取出来
- 看看这是什么错误,好像特别弱!
- IIS7中.net Web程序不能加载托管的DLL
- asp.net 怎么给word execel ppt 的第一页生成预览图
2 {
3 string strImageID = Request.QueryString["id"];
4 SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=sa;");
5 SqlCommand myCommand = new SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID="
6 + strImageID, myConnection);
7
8 try
9 {
10 myConnection.Open();
11 SqlDataReader myDataReader;
12 myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
13 if(myDataReader.Read())
14 {
15 Response.Clear();
16
17 //Response.ContentType = myDataReader["PersonImageType"].ToString();
18 Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
19 }
20 myConnection.Close();
21 }
22 catch (SqlException SQLexc)
23 {
24 Response.Write(SQLexc.ToString());
25 }
26 //Response.End();
27 }
{
//调用方法如:ShowImage("select Photo from UserPhoto where UserNo='" + userno +"'");
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
byte[] b= (byte[])cmd.ExecuteScalar();
if (b.Length 〉 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
pictureBox1.Image = new Bitmap(stream);
stream.Close();
}
conn.Close();
}
//sql:select Photo from UserPhoto where ID=@ID
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
byte[] b= (byte[])cmd.ExecuteScalar();
context.Response.BinaryWrite(b);
context.Response.ContentType = "image/jpeg";
context.Response.End();
star Tim 要我让你把代码写的能看懂点儿、、、就是说 。 <img src="ashx/GetImage.ashx?ID=1" />
这句啥子意思
就是去请求GetImage.ashx,通过ID返回你的image字段的值。然后输出图片的二进制。
{
string strsql;
DataSet myDs;
try
{
strsql="select BookType.Name as BookTypeName,book.id,book.name,author,price,type,publisher,Description,translator,discount,hits,status,sales,image=case when(not Cover is null) then ' <img src=Readimage.aspx?id='+cast(book.id as varchar(10))+' Border=1 width=80 height=120>' else ' <img src=img/pic.jpg border=1 width=80 height=120>' end from book join BookType on book.type=booktype.id where Book.id="+BookId;
myDs=ExecuteSql4Ds(strsql);
return myDs.Tables[0].Rows[0];
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}}
//读取二进制流的Readimage.aspx页面代码:
DataView myDv;
int id=int.Parse(Request.QueryString["id"].Trim());
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=bookshop;Integrated Security=True");
string cmdText = "select cover from book where id="+id+"";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmdText,con);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
myDv = ds.Tables[0].DefaultView;
con.Close();
try
{
Response.ContentType = "image/*";
Response.BinaryWrite((byte[])myDv[0]["Cover"]);
}
catch
{
Response.Write("/img/pic.jpg");
}