| 发表于:2007-01-22 21:00:3524楼 得分:0 |
恩恩 。。。好 的。。谢谢了。。。 private void 新增学生表_load(object sender, system.eventargs e) { } private void button1_click(object sender, system.eventargs e) //浏览要添加的图片 { if(this.openfiledialog1.showdialog()==dialogresult.ok) { try { this.picturebox1.image=image.fromfile(this.openfiledialog1.filename); } catch(exception x) { messagebox.show( "图片的像素过大 "+x.message); } } } private void button2_click(object sender, system.eventargs e) //存储sql图片 { //this.picturebox1.image=null; try { byte[] photo = getphoto(this.openfiledialog1.filename); sqlconnection nwindconn = new sqlconnection( "server=.;database=system;uid=sa;pwd=; "); sqlcommand addemp = new sqlcommand( "insert into table1(aa) values( ' "+photo+ " ') ", nwindconn); nwindconn.open(); addemp.EXECutenonquery(); nwindconn.close(); } catch(sqlexception x) { messagebox.show(x.message); } } public static byte[] getphoto(string filepath) //函数 { filestream fs = new filestream(filepath, filemode.open, fileaccess.read); binaryreader br = new binaryreader(fs); byte[] photo = br.readbytes((int)fs.length); br.close(); fs.close(); return photo; } private void button3_click(object sender, system.eventargs e) //读取 sql图片 { try { string connectionstring = "server=.;uid=sa;pwd=;database=system "; sqlconnection conn=new sqlconnection(connectionstring); string sql= "select aa from table1 where bb= 'keling ' "; sqlcommand command = new sqlcommand(sql,conn); conn.open(); sqldatareader dr = command.EXECutereader(commandbehavior.sequentialaccess); if(dr.read()) { //byte[] image =(byte[])dr[0]; //memorystream ms = new memorystream(((byte[])dr[ "aa "])); //this.picturebox1.image = image.fromstream(ms,true); byte[] image =(byte[])dr[ "aa "]; memorystream mystream = new memorystream(image, true); mystream.write(image, 0, image.length); this.picturebox1.image = image.fromstream(mystream); } else { messagebox.show( "没有成功读入数据! ") ; } conn.close(); } catch(exception x) { messagebox.show(x.message); } } | | |
|