MySQL保存jpg 圖片的實際操作過程
作者:佚名 
  我們今天主要向大家描述的是MySQL保存jpg 圖片的實際操作流程,以及對實現(xiàn)MySQL保存jpg 圖片的實際應(yīng)用代碼的介紹。
 以下的文章主要介紹的是MySQL保存jpg 圖片的實際操作過程,我們大家都知道MySQL數(shù)據(jù)庫下可以通過blob, mediumbolb,l ongblob等一些類型來保存圖片,當(dāng)然不同的相關(guān)圖片文件類似操作會有所不同,例如.bmp格式圖片。
示例代碼:
保存圖片到MySQL
- private void btnOpenFile_Click(object sender, EventArgs e)
 - {
 
打開圖片文件
- this.openFileDialog1.InitialDirectory = "C:\\";
 - this.openFileDialog1.FileName = "";
 - this.openFileDialog1.ShowDialog();
 
連接字符串
- string connStr = "server=vitus;User Id=root;Password=******;Persist Security Info=True;database=Test";
 - string sql = string.Format("insert into ImageTest values(@id,@picture)");
 - FileStream fs = new FileStream(this.openFileDialog1.FileName,FileMode.Open);
 - Byte[] bts = new Byte[fs.Length-1];
 - fs.Read(bts,0,(int)fs.Length-1);
 - MySqlConnection sqlConn = new MySqlConnection(connStr);
 - MySqlCommand sqlComm = new MySqlCommand(sql,sqlConn);
 - sqlComm.Parameters.Add("@id", MySqlDbType.Int32, 1);
 - sqlComm.Parameters["@id"].Value = 2;
 - sqlComm.Parameters.AddWithValue("@picture", bts);
 - sqlConn.Open();
 - sqlComm.ExecuteNonQuery();
 - sqlConn.Clone();
 - }
 
從MySQL中讀取并顯示圖片
- private void btnImageView_Click(object sender, EventArgs e)
 - {
 - string connStr = "server=vitus;User Id=root;Password=******;Persist Security Info=True;database=Test";
 - string sql = string.Format("select * from ImageTest where id=2");
 - MySqlConnection sqlConn = new MySqlConnection(connStr);
 - MySqlCommand sqlComm = new MySqlCommand(sql, sqlConn);
 - sqlConn.Open();
 - MySqlDataReader dr = sqlComm.ExecuteReader(CommandBehavior.CloseConnection);
 - Image image = null;
 - while (dr.Read())
 - {
 - MemoryStream buff = new MemoryStream((byte[])dr[1]);
 - image = Image.FromStream(buff, true);
 - buff.Close();
 - }
 - this.pictureBox1.Image = image;
 - }
 
以上的相關(guān)內(nèi)容就是對MySQL保存jpg圖片的介紹,望你能有所收獲。
【編輯推薦】
- MySQL 游標(biāo)的具體使用方案
 - MySQL show的實際操作用法
 - MySQL數(shù)據(jù)庫中的5種數(shù)據(jù)類型簡介
 - MySQL導(dǎo)入導(dǎo)出.sql文件實踐演練
 - MySQL root密碼忘記的解決
 
責(zé)任編輯:佚名 
                    來源:
                    互聯(lián)網(wǎng)
 














 
 
 
 
 
 
 