<<  < 2013 - 12 >  >>
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31




将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下: 

    private void button1_Click(object sender, EventArgs e)

        {

          openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

            if(openFileDialog1.ShowDialog()==DialogResult.OK)

            {

              string fullpath =openFileDialog1.FileName;//文件路径

              FileStream fs = new FileStream(fullpath, FileMode.Open);

                byte[] imagebytes =new byte[fs.Length];

                BinaryReader br = new BinaryReader(fs);

                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));

                //打开数据库

                SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

                con.Open();

                SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);

                com.Parameters.Add("ImageList", SqlDbType.Image);

                com.Parameters["ImageList"].Value = imagebytes;将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下: 

    private void button1_Click(object sender, EventArgs e)

        {

          openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

            if(openFileDialog1.ShowDialog()==DialogResult.OK)

            {

              string fullpath =openFileDialog1.FileName;//文件路径

              FileStream fs = new FileStream(fullpath, FileMode.Open);

                byte[] imagebytes =new byte[fs.Length];

                BinaryReader br = new BinaryReader(fs);

                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));

                //打开数据库

                SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

                con.Open();

                SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);

                com.Parameters.Add("ImageList", SqlDbType.Image);

                com.Parameters["ImageList"].Value = imagebytes;
将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下: 

    private void button1_Click(object sender, EventArgs e)

        {

          openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

            if(openFileDialog1.ShowDialog()==DialogResult.OK)

            {

              string fullpath =openFileDialog1.FileName;//文件路径

              FileStream fs = new FileStream(fullpath, FileMode.Open);

                byte[] imagebytes =new byte[fs.Length];

                BinaryReader br = new BinaryReader(fs);

                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));

                //打开数据库

                SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

                con.Open();

                SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);

                com.Parameters.Add("ImageList", SqlDbType.Image);

                com.Parameters["ImageList"].Value = imagebytes;
                com.ExecuteNonQuery();

                 con.Close();

              }     

 } 
实现本实例主要是利用SqlDataReader从数据库中把Image字段值读出来,赋给一个byte[]字节数组,然后使用MemoryStream类与Bitmap把图片读取出来。主要代码如下:

    private void button1_Click(object sender, EventArgs e)

        {

                 byte[] imagebytes = null;

                //打开数据库

            SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

                con.Open();

                SqlCommand com = new SqlCommand("*** top 1* from tb_09", con);

                SqlDataReader dr = com.ExecuteReader();

                while (dr.Read())

                {

                    imagebytes = (byte[])dr.GetValue(1);

                }

                dr.Close();

                com.Clone();

                con.Close();

                MemoryStream ms = new MemoryStream(imagebytes);

                Bitmap bmpt = new Bitmap(ms);
               pictureBox1.Image = bmpt;

        }


  • 标签:图片 数据库 
  • 发表评论:
    天涯博客欢迎您!