當前位置:首頁 » 存儲配置 » 資料庫如何存儲圖片路徑

資料庫如何存儲圖片路徑

發布時間: 2022-11-28 19:35:58

Ⅰ 圖片如何存入資料庫

通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
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);
//將路徑保存到變數,將該變數的值保存到資料庫相應欄位即可
uppath = "~/upload/" + dataName + "." + type;
}
二、將圖片以二進制數據流直接保存到資料庫:
引用如下命名空間:
using System.Drawing;
using System.IO;
using System.Data.sqlClient;
設計資料庫時,表中相應的欄位類型為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 )";//操作資料庫語句根據需要修改
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();
讀取:
...連接資料庫字元串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定從資料庫讀取出來的圖片的保存路徑及名字
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;
採用倆種方式可以根據實際需求靈活選擇。

Ⅱ 圖片如何存入資料庫

1、新建一個資料庫,資料庫名為Image,表名為image。並為表添加ID,tupian兩個列。

Ⅲ 如何將圖片路徑存入資料庫 圖片路徑是放在哪的路徑

圖片的路徑通常是放在伺服器里的,將路徑存入資料庫的話很簡單,在資料庫設置一個varchar型的欄位就可以了。 資料庫是通過伺服器進行連接的,不用上傳的;要圖片顯示出來的話,當然要讀出資料庫相應得欄位獲取字元串,也就是圖片的路徑。

Ⅳ 如何將圖片儲存在MySQL資料庫里

解決方法一般有兩種:

1、將圖片保存的路徑存儲到資料庫;

2、將圖片以二進制數據流的形式直接寫入資料庫欄位中。

以下為具體方法:

一、保存圖片的上傳路徑到資料庫:
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);
//將路徑保存到變數,將該變數的值保存到資料庫相應欄位即可
uppath
= "~/upload/" + dataName + "." +
type;
}
二、將圖片以二進制數據流直接保存到資料庫:
引用如下命名空間:
using
System.Drawing;
using System.IO;
using
System.Data.SqlClient;
設計資料庫時,表中相應的欄位類型為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
)";//操作資料庫語句根據需要修改
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();
讀取:
...連接資料庫字元串省略
mycon.Open();
SqlCommand
command = new
SqlCommand("select stuimage from stuInfo where stuid=107",
mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar
();
//指定從資料庫讀取出來的圖片的保存路徑及名字
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;
採用這兩種方式可以根據實際需求靈活選擇。

Ⅳ SQL中怎麼存圖片路徑

不知道說的對不對。
如果你是想在資料庫中保存圖片的路徑的話,完全可以用varchar類型的,然後保存一個項目中圖片的相對路徑即可。
但如果你是想把整個圖片都保存到資料庫的話那就只得用image類型了。
建議不要在資料庫中保存圖片,那樣讀寫很費資源。

Ⅵ mysql資料庫怎麼用sql指令存入圖片路徑

  • 在資料庫中,創建一張data表,用於測試。

Ⅶ 資料庫中可以存儲照片么怎麼存儲

數據中可以存儲圖片,但是需要注意不能直接存儲圖片,而是轉換成二進制或者Base64等的「文本」來存儲,在用的時候,可以再轉換回來。
在網站開發中,一般將圖片存儲在文件系統中,而不是數據系統中,資料庫系統中只記錄圖片在文件系統中的路徑而已。

Ⅷ 資料庫怎麼儲存圖片

資料庫存儲圖片,其實是存儲圖片在伺服器上的路徑或圖片的絕對地址 。它是一個字元串,所以資料庫欄位的類型可使用varchar【可變的,長度不超過255】。在前台調用時,需要將路徑放置在img標簽的src屬性中,即可顯示圖片

Ⅸ 資料庫如何存儲圖片和取出圖片

思路是這樣的!你在個人資料裡面加個欄位
varchar類型的,用來保存圖片路徑。然後把圖片名稱保存到資料庫就行了,取的話就用Sql讀出圖片名稱,把要放置圖片的位置路徑寫好,名稱就用那個欄位拼下字元串就行了!我是學Java的。C#代碼不是很熟,思路就這樣的吧。我以前就這樣做的,你可以試下!

熱點內容
編程貓前端 發布:2025-09-23 06:03:09 瀏覽:870
wampphp下載 發布:2025-09-23 06:01:54 瀏覽:555
網路存儲25 發布:2025-09-23 05:56:53 瀏覽:586
c語言敲代碼 發布:2025-09-23 05:55:25 瀏覽:732
協方差運演算法則 發布:2025-09-23 05:09:46 瀏覽:389
java數組靜態初始化 發布:2025-09-23 05:06:51 瀏覽:554
創建存儲函數的關鍵詞是 發布:2025-09-23 04:37:33 瀏覽:942
easyui框架源碼 發布:2025-09-23 04:28:52 瀏覽:479
啪啪游戲廳存儲位置 發布:2025-09-23 04:06:01 瀏覽:982
光遇伺服器人數過多怎麼辦 發布:2025-09-23 03:59:20 瀏覽:444