如何将图片以Image类型存储到数据库 如何从数据库中读取Image类型的字段

 将图片以Image类型存储到数据库中,首先获取图片的流对象,然后,利通Read方法从图片文件读取二进制数据存储到字节数组。
从数据库中读取Image类型的字段,首先将数据库对应的Image类型字段存储在无符号直接数组,然后再用MemoryStream类读入图片数据,通过MemoryStream对象生成Bitmap对象,显示在PictureBox控件上。代码如下:

   private void butOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Image Files(*.bmp;*.jpg;*.gif)|*.BMP;*.JPG;*.GIF";
            of.DefaultExt = "bmp";
            of.ShowDialog();
            if (of.FileName.Length > 0)
            {
                using (Stream sr = of.OpenFile())
                {
                    int length = (int)sr.Length;
                    byte[] b = new byte[length];
                    sr.Read(b, 0, length);
                    pictureBox1.Image = new Bitmap(sr);
                    ///Save
                    try
                    {
                        using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=db_13"))
                        {
                            SqlDataAdapter ap = new SqlDataAdapter("select * from tb_48", con);
                            SqlCommandBuilder cmd = new SqlCommandBuilder(ap);
                            ap.UpdateCommand = cmd.GetUpdateCommand();
                            DataSet ds = new DataSet();
                            ap.Fill(ds);
                            DataRow newRow = ds.Tables[0].NewRow();
                            newRow.BeginEdit();
                            newRow["Name"] = of.FileName;
                            newRow["Image"] = b;
                            newRow.EndEdit();
                            ds.Tables[0].Rows.Add(newRow);
                            ap.Update(ds);
                            MessageBox.Show("Save Succesful");
                        }

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }



        private void butLoad_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=db_13"))
                {
                    SqlDataAdapter ap = new SqlDataAdapter("select top 1* from tb_48 order by  ID desc ", con);
                    SqlCommandBuilder cmd = new SqlCommandBuilder(ap);
                    ap.UpdateCommand = cmd.GetUpdateCommand();
                    DataSet ds = new DataSet();
                    ap.Fill(ds);
                    byte[] b = (byte[])ds.Tables[0].Rows[0]["Image"];
                    using (MemoryStream sr = new MemoryStream(b))
                    {
                        pictureBox1.Image = new Bitmap(sr);
                    }
                    MessageBox.Show("Load Succesful");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

你可能感兴趣的:(exception,数据库,image,database,存储,dataset)