保存图片到数据库 与 数据库获取图片,显示在PictureBox控件中。

 //保存片到数据
        private void btnSetPhoto_Click(object sender, EventArgs e)
        {
            //声明存放片的字
            byte[] photo;
            //打要保存片的文件流
            FileStream fs = new FileStream("Scence.bmp", FileMode.Open, FileAccess.Read);
            //建字流,文件流取操作
            BinaryReader br = new BinaryReader(fs);
            //通取文件,并返回
            photo = br.ReadBytes((int)fs.Length);
 
            //关闭
            br.Close();
            //关闭文件流
            fs.Close();
            //建数据库连
            SqlConnection conn = new SqlConnection(ConnectionConfig.getConnectionString2());
            //组织SQL句,将片信息的存以参数的方式
            string strSql = "Update stuInfo Set stuPhoto = @stuPhoto Where stuNo =‘QST001‘";
            //建Command命令
            SqlCommand comm = new SqlCommand();
            //置Command的
            comm.Connection = conn;
            //置Command要行的SQL
            comm.CommandText = strSql;
            //添加片参数信息
            SqlParameter para = comm.Parameters.Add("@stuPhoto", SqlDbType.Image);
            //片的字参数
            para.Value = photo;
            //打开连
            conn.Open();
            //行SQL句,返回影响数
            int numUpdate = comm.ExecuteNonQuery();
            //关闭连
            conn.Close();
 
            richTextBox1.Text = string.Format("SQL句“{0}”被行!果如下: \r", strSql);
            richTextBox1.Text = String.Format("更新了{0}条记录。", numUpdate.ToString());
 
        }

 

        //数据库获片,示在PictureBox控件中。
        private void btnReadPhoto_Click(object sender, EventArgs e)
        {
            //建数据库连
            SqlConnection conn = new SqlConnection(ConnectionConfig.getConnectionString2());
            //组织SQL
            string strSql = "Select stuPhoto  from stuInfo Where stuNo =‘QST001‘";
            //建Command命令
            SqlCommand comm = new SqlCommand();
            //置Command的
            comm.Connection = conn;
            //置Command要行的SQL
            comm.CommandText = strSql;
            //打开连
            conn.Open();
            //行SQL句,并返回SqlDataReader信息,参数表示:提供一方法,以便 DataReader 理包含有大二的列的行。
            SqlDataReader reader = comm.ExecuteReader(CommandBehavior.SequentialAccess);
 
            StringBuilder strResult = new StringBuilder();
            //取数据流
            while (reader.Read())
            {
                //置字节缓存大小
                int buffersize = 100;
                //取二制的字索引位置
                int startindexer = 0;
                //取字
                long retval;
                //冲字
                Byte[] buffer = new Byte[buffersize];
                //建内存流,将片信息取到内存中保存
                MemoryStream ms=new MemoryStream();
                //用GetBytes()方法片的信息,参数分表示:
                                                          //列序号;
                                                          //字段中的索引,从其取操作;
                                                          //要将字入的冲区;
                                                          //buffer 中写入操作始位置的索引;
                                                          //要制到冲区中的最大度。
                //返回表示:取的实际
                retval = reader.GetBytes(0, startindexer, buffer, 0, buffersize);
                //继续读取信息,取100字,指导实际读取字不是100字节为
                while (retval == buffersize)
                {
                    //将字信息写于内存流中,在内存中存放片的二制信息
                    ms.Write(buffer, 0, buffersize);
                    
                    ms.Flush();
 
                    startindexer += buffersize;
                    //取数据中的二片信息,取100字
                    retval = reader.GetBytes(0, startindexer, buffer, 0, buffersize);
                }
 
                ms.Write(buffer, 0, (int)retval );
                ms.Flush();
 
               
                //将内存流中的片二制信息,生成Bitmap
                Bitmap image = new Bitmap(ms);
                //在片框控件中,示内存中的片信息
                this.picPhoto.Image = image;
 
                //关闭内存流
                ms.Close();
 
            }
 
            //关闭SqlDataReader数据流
            reader.Close();
            //关闭连
            conn.Close();
        }

保存图片到数据库 与 数据库获取图片,显示在PictureBox控件中。

上一篇:关于贫穷和拖延的天才发现【摘】


下一篇:midwayjs 使用egg-mysql