private void readimagebtn_click(object sender,system.eventargs e)
{
//构造一个SQL字符串和一个连接对象
string sql="select * from users ";
oledbconnection conn=new oledbconnection();
conn.connectionstring=connectionstring;
//打开连接
if (conn.state!=connectionstate.open)
conn.open();
oledbdataadapter adapter= new oledbdataadapter (sql,conn);
oledbcommandbuilder cmdbuilder=new oledbcommandbuilder (adapter);
dataset ds =new dataset("users");
adapter.missingschemaaction=missingschemaaction.addwithkey;
//填充数据适配器
adapter.fill(ds,"users");
//获取表的第一行
datarow row=ds.tables["users"].rows[0];
//读取流中的数据
byte[] rawdata=new byte[0];
rawdata=(byte[])row["userphoto"];
int len=new int();
len=rawdata.getupperbound(0);
//将rawdata保存为位图
filestream fs=new filestream(saveimagename,filemode.openorcreate,fileaccess.write);
fs.write(rawdata,0,len);
//关闭流
fs.close();
//在图片筐中显示图像
image curimage=image.fromfile(saveimagename);
picturebox1.image=curimage;
//清除集合
if (conn!=null)
{
if (conn.state==connectionsate.open)
cpmm.close();
//释放连接
conn.dispose();
}
}
这段代码展示了如何从数据库中读取存储的图像数据。首先,通过SQL查询获取数据,然后使用数据适配器填充数据集。接着,将字节数组转换回图像,并保存到本地文件,最后在PictureBox中显示该图像。
1517

被折叠的 条评论
为什么被折叠?



