C# 图像保存到数据库 & 从数据库里读出显示

原创
2020/11/03 17:23
阅读数 44
        private static byte[] FileToBytes(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);
            byte[] buffer = new byte[fi.Length];
            FileStream fs = fi.OpenRead();
            fs.Read(buffer, 0, Convert.ToInt32(fi.Length));
            fs.Close();
            return buffer;
        }

        private static void CreateFile(byte[] fileBuffer, string newFilePath)
        //用文件流生成一个文件 
        {
            if (File.Exists(newFilePath))
            {
                File.Delete(newFilePath);
            }
            FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(fileBuffer, 0, fileBuffer.Length); //用文件流生成一个文件 
            bw.Close();
            fs.Close();
        }

        private static MySqlConnection CreateConnection()
        {
            string host = "localhost";
            string id = "root";
            string pwd = "1234567";
            string database = "txl";
            string connectionStr = string.Format("Server={0};port={1};Database={2};UserID={3};Password={4};", host, "3306", database, id, pwd);
            MySqlConnection Connection = new MySqlConnection(connectionStr); //建立MySQL连接 
            return Connection;
        }


        private static void SendFileBytesToDatabase(string aNO, byte[] fileArr)
        {
            MySqlConnection con = CreateConnection();
            string sendFileSql = "update t_member set photo=@photo where no=@NO;";
            MySqlCommand Cmd = new MySqlCommand(sendFileSql, con);
            Cmd.Parameters.Add("@NO", MySqlDbType.VarChar).Value = aNO;
            Cmd.Parameters.Add("@photo", MySqlDbType.MediumBlob).Value = fileArr;
            
            con.Open();
           
            try
            {
                Cmd.ExecuteNonQuery();               
            }
            catch (Exception e)
            {
                MessageBox.Show("向数据库存储数据失败:" + e.Message);
            }
            finally
            {
                con.Close();
            }
        }  


        private void showPhoto(int aNO)
        {
            MySqlConnection con = CreateConnection();
            string Sql = "select photo from t_member where no=" + aNO;
            MySqlCommand Cmd = new MySqlCommand(Sql, con);
            con.Open();
            MySqlDataAdapter adt = new MySqlDataAdapter(Cmd);
            DataSet dst = new DataSet();
            adt.Fill(dst);
            if (dst.Tables[0].Rows.Count == 1)
            {
                if(dst.Tables[0].Rows[0]["Photo"]==DBNull.Value)
                {
                    pictureBox1.Image = null;
                }else
                {
                    byte []buf= (byte[])(dst.Tables[0].Rows[0]["Photo"]);
                    pictureBox1.Image = Image.FromStream(new MemoryStream(buf)); 
                }
            }
            con.Close();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Load(openFileDialog1.FileName);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string imgPath = @"c:\grill.jpg";
            byte[] imgBuffer = FileToBytes(imgPath);
            SendFileBytesToDatabase("5", imgBuffer);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string imgPath = @"D:\Grill7.jpg";
            // GetFileFromDatabase("7", imgPath); 
            showPhoto(1);
        }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部