將圖片儲存在MySQL數(shù)據(jù)庫中的幾種方法
通常對用戶上傳的圖片需要保存到數(shù)據(jù)庫中。
解決方法一般有兩種:
1、將圖片保存的路徑存儲到數(shù)據(jù)庫;
2、將圖片以二進制數(shù)據(jù)流的形式直接寫入數(shù)據(jù)庫字段中。
以下為具體方法:
一、保存圖片的上傳路徑到數(shù)據(jù)庫:
string uppath="";//用于保存圖片上傳路徑 //獲取上傳圖片的文件名 string fileFullname = this.FileUpload1.FileName; //獲取圖片上傳的時間,以時間作為圖片的名字可以防止圖片重名 string dataName = DateTime.Now.ToString("yyyyMMddhhmmss"); //獲取圖片的文件名(不含擴展名) string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1); //獲取圖片擴展名 string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1); //判斷是否為要求的格式 if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF") { //將圖片上傳到指定路徑的文件夾 this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type); //將路徑保存到變量,將該變量的值保存到數(shù)據(jù)庫相應字段即可 uppath = "~/upload/" + dataName + "." + type; }
二、將圖片以二進制數(shù)據(jù)流直接保存到數(shù)據(jù)庫:
引用如下命名空間:
using System.Drawing; using System.IO; using System.Data.SqlClient; 設計數(shù)據(jù)庫時,表中相應的字段類型為iamge 保存: //圖片路徑 string strPath = this.FileUpload1.PostedFile.FileName.ToString (); //讀取圖片 FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); byte[] photo = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); //存入 SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123"); string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作數(shù)據(jù)庫語句根據(jù)需要修改 SqlCommand myComm = new SqlCommand(strComm, myConn); myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length); myComm.Parameters["@photoBinary"].Value = photo; myConn.Open(); if (myComm.ExecuteNonQuery() > 0) { this.Label1.Text = "ok"; } myConn.Close(); 讀?。? ...連接數(shù)據(jù)庫字符串省略 mycon.Open(); SqlCommand command = new SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查詢語句根據(jù)需要修改 byte[] image = (byte[])command.ExecuteScalar (); //指定從數(shù)據(jù)庫讀取出來的圖片的保存路徑及名字 string strPath = "~/Upload/zhangsan.JPG"; string strPhotoPath = Server.MapPath(strPath); //按上面的路徑與名字保存圖片文件 BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate)); bw.Write(image); bw.Close(); //顯示圖片 this.Image1.ImageUrl = strPath; //采用這兩種方式可以根據(jù)實際需求靈活選擇。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接
- MySQL中可為空的字段設置為NULL還是NOT NULL
- MySQL中字段類型char、varchar和text的區(qū)別
- MySQL數(shù)據(jù)庫遷移快速導出導入大量數(shù)據(jù)
- shell腳本操作mysql數(shù)據(jù)庫刪除重復的數(shù)據(jù)
- MySQL實現(xiàn)類似Oracle序列的方案
- mysql實現(xiàn)sequence功能的代碼
- Can''t connect to local MySQL through socket ''/tmp/mysql.sock''解決方法
- Mysql常用函數(shù)大全(分類匯總講解)
- 利用MySQL主從配置實現(xiàn)讀寫分離減輕數(shù)據(jù)庫壓力
- MySQL關于sql_mode解析與設置講解
相關文章
MySQL 發(fā)生同步延遲時Seconds_Behind_Master還為0的原因
騰訊云數(shù)據(jù)庫 MySQL 的只讀實例出現(xiàn)了同步延遲,但是監(jiān)控的延遲時間顯示為 0,而且延遲的 binlog 距離非 0,且數(shù)值越來越大。臨時解決之后,仔細想了一想,Seconds_Behind_Master 雖然計算方式有點坑,但是出現(xiàn)這么“巨大”的誤差還是挺奇怪的,本文就來分析下這個問題2021-06-06MySQL錯誤提示:sql_mode=only_full_group_by完美解決方案
有時候遇到數(shù)據(jù)庫重復數(shù)據(jù),需要將數(shù)據(jù)進行分組,并取出其中一條來展示,這時就需要用到group by語句,下面這篇文章主要給大家介紹了關于MySQL錯誤提示:sql_mode=only_full_group_by的完美解決方案,需要的朋友可以參考下2022-10-10mysql xtrabackup 備份恢復實現(xiàn)分享
Xtrabackup是由percona提供的mysql數(shù)據(jù)庫備份工具,據(jù)官方介紹,這也是世界上惟一一款開源的能夠?qū)nnodb和xtradb數(shù)據(jù)庫進行熱備的工具2012-11-11SQL實現(xiàn)對數(shù)據(jù)庫檢索數(shù)據(jù)的直接轉(zhuǎn)換計算
這篇文章主要介紹了SQL實現(xiàn)對數(shù)據(jù)庫檢索數(shù)據(jù)的直接轉(zhuǎn)換計算,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09