C#存储图片到数据库中的Image字段

发布时间:2022-11-24 03:53:51   来源:文档文库   
字号:
字号:大privatevoidbutton1_Click(objectsender,System.EventArgse{stringfilename=textBox1.Text;//图片文件所在的路径BinaryReaderreader=null;FileStreammyfilestream=newFileStream(filename,FileMode.Open;try{reader=newBinaryReader(myfilestream;byte[]image=reader.ReadBytes((intmyfilestream.Length;//存储图片到数组中。using(SqlConnectionconn=newSqlConnection("server=test05;database=esdb2;uid=datatran;pwd=qyrl"{using(SqlCommandcommand=conn.CreateCommand({command.CommandText=@"INSERTINTOphoto(photoVALUES(@photo";command.Parameters.Add("@photo",image;conn.Open(;command.ExecuteNonQuery(;conn.Close(;MessageBox.Show("文件保存成功!","提示",MessageBoxButtons.OK,MessageBoxIc
on.Information;}}}catch(IOExceptionee{MessageBox.Show(ee.Message.ToString(;}finally{if(reader!=nullreader.Close(;}}privatevoidbutton2_Click(objectsender,System.EventArgse{//把数据表中Image字段中保存的图片信息转换图片形式在Piceture控件中显示出来。using(SqlConnectionconn=newSqlConnection("server=test05;database=esdb2;uid=datatran;pwd=qyrl"{using(SqlCommandcommand=conn.CreateCommand({command.CommandText=@"selecttop1photofromphotowherePid=@Pid";command.Parameters.Add("@id",textBox2.Text;conn.Open(;

本文来源:https://www.2haoxitong.net/k/doc/d251db868762caaedd33d4df.html

《C#存储图片到数据库中的Image字段.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式