C#显示SQL图片,插入SQL图片,删除SQL图片

来源:互联网 发布:北国知春物业 编辑:程序博客网 时间:2024/04/30 05:08

原代码下载地址:http://download.csdn.net/detail/xiongyongting/9712303


使用工具VS2015,SQLSERVER2008R2
 //插入图片
        public void insertPP()
        {
            openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
            SqlConnection con = new SqlConnection(conmmsy);
            con.Open();
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string path = openFileDialog1.FileName;//文件路径                    
                    //创建文件流,path参数是文件路径  
                    FileStream fs = new FileStream(path, FileMode.Open);
                    int streamLength = (int)fs.Length;  //获取文件流的长度。  
                    byte[] image = new byte[streamLength];    //声明字节数组,用于保存图片文件  
                    fs.Read(image, 0, streamLength);    //把图片文件转换成为字节数组保存  
                    fs.Close();
                    //插入Sql语句,@img是Sql语句参数。  
                    string sql = string.Format("insert imagetable values(@img)", image);
                    SqlCommand com = new SqlCommand(sql, con); //con是一个有效的连接对象  
                                                               //为命令对象添加参数,注意参数的类型  
                    com.Parameters.Add(new SqlParameter("img", SqlDbType.Binary, image.Length,
                    ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));
                    com.ExecuteNonQuery();   //执行  
                    MessageBox.Show("插入成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "插入图片不成功");
            }
            finally
            {
                con.Close();
            }
        }

public void testShowPP()

        {
            SqlConnection con2 = new SqlConnection(conmmsy);
            con2.Open();
            int i = 0;
            try
            {
               
                string sql = "select id,imagetxt from imagetable";
                //创建命令对象,con参数是一个连接对象  
                SqlCommand com = new SqlCommand(sql, con2);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    //声明数组,用于保存数据库的二进制数据  
                    i = i + 1;
                    byte[] mybyte = null;
                    //读取数据保存到数组中  
                    mybyte=(byte[])dr["imagetxt"];
                    Image image;
                    //读取数组数据成为文件流  
                    MemoryStream mymemorystream = new MemoryStream(mybyte);
                    //转换成为图片格式。  
                    image = Image.FromStream(mymemorystream, true);
                    this.pictureBox1.Image = image;                    
                    mymemorystream.Close();  //关闭流  
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "显示图片失败");
            }
            finally { con2.Close(); }

        }

 public void updatePP(string id)
        {
            openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
            SqlConnection con = new SqlConnection(conmmsy);
            con.Open();
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string path = openFileDialog1.FileName;//文件路径                    
                    //创建文件流,path参数是文件路径  
                    FileStream fs = new FileStream(path, FileMode.Open);
                    int streamLength = (int)fs.Length;  //获取文件流的长度。  
                    byte[] image = new byte[streamLength];    //声明字节数组,用于保存图片文件  
                    fs.Read(image, 0, streamLength);    //把图片文件转换成为字节数组保存  
                    fs.Close();
                    //插入Sql语句,@img是Sql语句参数。  
                    string sql = string.Format("update imagetable set imagetxt=(@img) where id='"+id+"'", image);
                    SqlCommand com = new SqlCommand(sql, con); //con是一个有效的连接对象  
                    //为命令对象添加参数,注意参数的类型  
                    com.Parameters.Add(new SqlParameter("img", SqlDbType.Binary, image.Length,
                    ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));
                    com.ExecuteNonQuery();   //执行  
                    MessageBox.Show("修改成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "修改图片不成功");
            }
            finally
            {
                con.Close();
            }
        }

0 0
原创粉丝点击