一、存储
1.得到一个图片对象
Image imgae = Image.FromFile(openFileDialog1.FileName);
2.创建内存流,并将图片保存为内存流
MemoryStream ms = new MemoryStream();
imgae.Save(ms, ImageFormat.Jpeg);
3.将该流转换成2进制数组
byte[] bb = ms.ToArray();
4.将该2进制插入到数据库对应的image列里
string sql = "insert image values(@a)";
SqlConnection con = new SqlConnection(ConStr);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@a", SqlDbType.Image).Value = bb;
con.Open();
cmd.ExecuteNonQuery();
二、读取 1.从数据库中读取image那一列,并将其取出后转换成2进制数组 DataTable dt = select("select * from image where id=1"); 2.用2进制创建一个内存流 MemoryStream ms = new MemoryStream(bb); 3.从内存流里读书图片 Image image=Image.FromStream(ms); 注意需要关闭流 |