/// <summary> /// 将物料图片存入数据库 /// </summary> /// <param name="MaterialID">料号</param> /// <param name="openF">包含图片信息的OpenFileDialog</param> public void SaveImage(string MaterialID, OpenFileDialog openF) { SqlConnection conn = Cls_DBConnection.MyConnection_MCP(); string strimg = openF.FileName.ToString(); //记录图片的所在路径 FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存 BinaryReader br = new BinaryReader(fs); byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中 conn.Open(); StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_MaterialInfo Set MaterialPhoto=@Photo where employeeID=" + MaterialID); SqlCommand cmd = new SqlCommand(strSql.ToString(), conn); cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn; cmd.ExecuteNonQuery(); conn.Close(); }
/// <summary> /// 将物料图片从数据库中取出 /// </summary> /// <param name="_MaterialID">物料编号</param> /// <param name="pb">PictureBox</param> public void Get_Image(string _MaterialID, PictureBox pb) { SqlConnection conn = Cls_DBConnection.MyConnection_MCP(); byte[] imagebytes = null; conn.Open(); SqlCommand com = new SqlCommand("select * from tb_MaterialInfo where MaterialID='" + _MaterialID + "'", conn); SqlDataReader dr = com.ExecuteReader(); while (dr.Read()) { imagebytes = (byte[])dr.GetValue(4); } dr.Close(); conn.Close(); MemoryStream ms = new MemoryStream(imagebytes); Bitmap bmpt = new Bitmap(ms); pb.Image = bmpt; }