当前位置: 移动技术网 > IT编程>开发语言>.net > c# 读取Northwind数据库image字段

c# 读取Northwind数据库image字段

2017年12月12日  | 移动技术网IT编程  | 我要评论

美女被虐吸奶小游戏,无忧技术组yy69007,小菲

这里值得一提的是,web控件image不像winform控件那样可以通过读取二进制流赋值给image属性来显示图像。可以通过变通的方法来实现,流行的做法是新建一个页面专门用来显示图像,这里代码直接用孟子e章前辈的(作了小修改,主要是剔除78个byte字节流来正常显示northwind数据库的图片):
readimage.aspx.cs
复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.data.sqlclient;

namespace webapplication2
{
public partial class readimage : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
string strimageid = request.querystring["id"];
sqlconnection myconnection = new sqlconnection("data source=.;initial catalog=northwind;user id=sa;password=123456;");
sqlcommand mycommand = new sqlcommand("select picture from categories where categoryid="
+ strimageid, myconnection);

try
{
myconnection.open();
sqldatareader mydatareader;
mydatareader = mycommand.executereader(commandbehavior.closeconnection);
if (mydatareader.read())
{
response.clear();

response.contenttype = "image/jpeg";
byte[] b = (byte[])mydatareader["picture"];
//下面的方法就是用来让图片可以正常显示
byte[] temp=new byte [b.length -78];
array.copy(b, 78, temp, 0, b.length - 78);
response.binarywrite(temp);
}
myconnection.close();
}
catch (sqlexception sqlexc)
{
response.write(sqlexc.tostring ());
}
response.end();

}
}
}

在源页面如default.aspx.cs可以通过下面方法调用
复制代码 代码如下:

protected void page_load(object sender, eventargs e)
{
if(!ispostback )
image1.imageurl = formaturl("1");

}
protected string formaturl(string strargument)
{
return "readimage.aspx?id=" + strargument;
}

如果不想新建一个页面来承载图像,也可以使用下面的方法:(注意:下面的类是自定义的,大家看得懂这个方法就可以了)
复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.data.sqlclient;

namespace webapplication2
{
public partial class readimage : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
string strimageid = request.querystring["id"];
sqlconnection myconnection = new sqlconnection("data source=.;initial catalog=northwind;user id=sa;password=123456;");
sqlcommand mycommand = new sqlcommand("select picture from categories where categoryid="
+ strimageid, myconnection);

try
{
myconnection.open();
sqldatareader mydatareader;
mydatareader = mycommand.executereader(commandbehavior.closeconnection);
if (mydatareader.read())
{
response.clear();

response.contenttype = "image/jpeg";
byte[] b = (byte[])mydatareader["picture"];
byte[] temp=new byte [b.length -78];
array.copy(b, 78, temp, 0, b.length - 78);
response.binarywrite(temp);
}
myconnection.close();
}
catch (sqlexception sqlexc)
{
response.write(sqlexc.tostring ());
}
response.end();

}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网