保存圖片的資料庫中
Ⅰ 怎樣將圖片存在資料庫裡面
一般圖像是不保存在資料庫的.而是先將圖片放在工程下的某個文件夾中,將圖片所在的工程文件路徑存在資料庫中,當程序載入圖片的時候,從資料庫中讀取圖片的路徑,然後根據路徑在工程的文件夾中讀取圖片文件
Ⅱ 圖片如何存入資料庫
通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
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;
採用倆種方式可以根據實際需求靈活選擇。
Ⅲ 如何將圖片存到資料庫中
一般圖片的處理都是上傳到伺服器然後將圖片的地址名稱依次保存在資料庫中,取出時按照地址取出就可以。直接用網上的圖片地址有的是可以的訪問,有的因為圖片加鎖,保留網上那個圖片地址最終是無法找到圖片的。
Ⅳ C#怎麼將圖片保存到SQL資料庫
this.pictureBox1.Image
=
Image.FromStream(this.openFileDialog1.OpenFile());
//獲取當前圖片的路徑
string
path
=
openFileDialog1.FileName.ToString();
//將制定路徑的圖片添加到FileStream類中
FileStream
fs
=
new
FileStream(path,
FileMode.Open,
FileAccess.Read);
//通過FileStream對象實例化BinaryReader對象
BinaryReader
br
=
new
BinaryReader(fs);
//通過BinaryReader類對象的ReadBytes()方法將FileStream類對象轉化為二進制數組
byte[]
imgBytesIn
=
br.ReadBytes(Convert.ToInt32(fs.Length));第二步:
//將圖片添加到資料庫中
string
sql="insert
into
pic
values(@pic)";
SqlParameter[]
param
=
new
SqlParameter[]
{
new
SqlParameter("@pic",
imgBytesIn)
};
DBHelper.GetExecuteQuery(sql,
param);第三步:
//將圖片從資料庫中取出
string
sql="select
*
from
pic
where
id=0";
SqlDataReader
reader
=
DBHelper.GetExecuteReader(sql,
null);
MemoryStream
mss
=
null;
Ⅳ 如何將圖片存到資料庫
通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
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;
採用倆種方式可以根據實際需求靈活選擇。
Ⅵ 怎麼把圖片保存到資料庫里
把你的圖片放在你項目的根目錄下面,把路徑保存在資料庫中。。資料庫一般不是用來放圖片的,如果你是做網站,你的空間根本不夠放那麼多。。建議你還是在資料庫中保存你圖片的地址
Ⅶ java如何將圖片保存在資料庫中
一般都是這樣的,就是在你伺服器有一個專門放置圖片的文件夾,然後資料庫保存的是你伺服器圖片的路徑。需要用的時候就去資料庫裡面取路徑。得到路徑以後你想怎麼處理圖片是你的事情了。
至於如何去資料庫取路徑這個就是簡單的db操作。
載入驅動類:
Class.forName(DBDriver);
獲取連接:
Connection
conn
=
DriverManager.getConnection(url,username,password);
創建操作對象:
PreparedStatement
stmt
=
con.prepareStatement(sql);
執行操作:
ResultSet
rs
=
stmt.executeQuery();
遍歷結果:
List
list
=
new
ArrayList();
while(rs.next()){
//具體操作,通常用rs.getString(name)取值
Image
img
=
new
Image();//圖片類對應你資料庫中圖片表格
img.setSrc(rs.getString("src"));//假設你資料庫中image表中圖片地址欄位是src
list.add(img);
}
記得關閉資源:
rs.close();
stmt.close();
con.close();
看你的意思是已經取出來了不知道怎麼顯示:
你取出來之後可以把圖片放在一個list裡面然後去頁面上遍歷這個list
<c:forEach
var="chakan1"
items="list">
<tr>
<td>
<img
src="${chakan1.src}"/>
</td>
</tr>
</c:forEach>
大致應該是這樣
Ⅷ 怎麼把圖片,視頻存儲在資料庫中
圖片、視頻一般都是存儲在磁碟中,然後把存儲在磁碟里的路徑存儲在資料庫中