日期:2014-05-19  浏览次数:20812 次

c#存图到数据库
如何把一张图片存到SQLSERVER数据库中,字段类型为IMAGE;
然后再将信息显示到pictureBox中.

------解决方案--------------------

朋友 看看这个问题。http://community.csdn.net/Expert/topic/5686/5686047.xml?temp=.8212702
------解决方案--------------------
//把图片读到pictureBox,再写入数据库

SqlConnection conn=new SqlConnection(@ "data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf ");
conn.Open();
SqlCommand cmd=new SqlCommand( "insert into fuser values ( '1a ', '1b ',@i) ",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@ "D:\windows temp\temp\1.jpg ",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add( "@i ",SqlDbType.Image,(int)fs.Length);
cmd.Parameters[ "@i "].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();

-------------------------------------------------------
//从数据库读图片到picturebox

SqlConnection conn=new SqlConnection(@ "data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf ");
conn.Open();
SqlCommand cmd=new SqlCommand( "select 照片 from fuser where password= '1b ' ",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;