从数据库中读取图像

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();

}

}

阅读更多
换一批

没有更多推荐了,返回首页