1. 程式人生 > 其它 >C#讀寫圖片檔案到Access資料庫中

C#讀寫圖片檔案到Access資料庫中

今天學習了把圖片檔案讀寫到資料庫中,我是用的Access資料庫,SQL還沒去測試,不過都差不多

資料庫表的樣式

練習嘛就隨便弄了下,說明下圖片轉成的字串要用備註型別才可以哦

如果用的Sql資料庫的話就用最大的字元型別吧

1。寫入圖片檔案到資料庫中

 //Access資料庫連線字串
        const string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MyAccessFile\MyPicte.accdb;Persist Security Info=False";

        string pic = string
.Empty; //儲存圖片轉化字串 //選擇圖片並寫入資料庫中 private void button1_Click(object sender, EventArgs e) { //開啟檔案對話方塊選擇圖片檔案 OpenFileDialog openfile = new OpenFileDialog(); openfile.Title = "請選擇圖片"; openfile.Filter="圖片(*.jpg;*.bmp;*.png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*
"; if (DialogResult.OK != openfile.ShowDialog()) return; try { //顯示圖片到PictureBox控制元件中 Bitmap bmp = new Bitmap(openfile.FileName); pictureBox1.Image = bmp; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
//把圖片轉化成二進位制,最後轉成字串 MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); pic = Convert.ToBase64String(arr); //把Byte位元組陣列轉成字串 //pic = ImageToString(openfile.FileName);//自己寫的圖片轉字串的函式封裝 //寫入資料庫中 OleDbConnection conn = new OleDbConnection(conStr); string sql = $"insert into mTable (pictName,pictText) values ('{openfile.FileName}','{pic}')"; OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); int res = cmd.ExecuteNonQuery(); if (res > 0) { MessageBox.Show("圖片儲存成功!"); } conn.Close(); conn.Dispose(); } catch (Exception) { throw; } }

2。從資料庫中讀取圖片檔案

//從資料庫中讀取圖片,並顯示在PictureBoc控制元件中
        private void button2_Click(object sender, EventArgs e)
        {
            int id = 6;//要讀取的圖片在資料庫中的編號,因為是測試就手動修改要讀取的編號
            try
            {
                OleDbConnection conn = new OleDbConnection(conStr);
                string sql = $"select pictText from mTable where ID={id}";
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
                DataTable dt = new DataTable();
                oda.Fill(dt);
                conn.Close();
                conn.Dispose();

                if(dt.Rows.Count>0)
                {
                    pic = dt.Rows[0][0].ToString();
                    if (!string.IsNullOrEmpty(pic))
                    {
                        //把string轉成位元組陣列
                        byte[] imageBytes = Convert.FromBase64String(pic);
                        //把位元組讀取到記憶體
                        MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
                        memoryStream.Write(imageBytes, 0, imageBytes.Length);

                        //位元組陣列轉成Image物件
                        Image image = Image.FromStream(memoryStream);

                        //Image image = StringToImage(pic);

                        //顯示圖片到控制元件中
                        this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                        this.pictureBox2.Image = image;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

學習成果展示:

好了今天就學到這了。

簽名:GreenLeaf1976