資料庫插入圖片
A. 圖片如何存入資料庫
通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
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;
採用倆種方式可以根據實際需求靈活選擇。
B. 在access資料庫中如何添加圖片
1、可以建立一個欄位 將其數據類型改為OLE對象 然後直接粘貼圖片
2、或用將圖片轉換為二進制 存入資料庫中
3、用普通欄位放一個路徑 指向圖片 直接讓顯示
第三種方法最好了 圖片直接存入資料庫,資料庫會很大的。。。
C. 如何在資料庫中添加圖片
我使用過,我一般是這么處理的,如下(我用的vb,你可以轉變成access里的代碼):
圖片是可以放到資料庫裡面的,以ACCESS為例子,要使用OLE數據類型,
然後把圖片轉化為長二進制數據存入,讀取的時候可以生成系統隱藏的圖
片然後在圖片控制項中顯示
Dim cc() As Byte
Dim i
Dim t
Private Sub Command1_Click()
CommonDialog1.Filter = "JPG圖片(*.BMP)|*.BMP"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Picture1.Picture = LoadPicture(CommonDialog1.FileName)
strname = CommonDialog1.FileName
Open strname For Binary As #1
ReDim cc(LOF(1) - 1)
t = cc(LOF(1) - 1) '在讀取二進制時,如果是用文件操作那麼涉及到到底從文件中取多少位元組的問題,所以位元組數組要指定大小
Get #1, , cc
Close
Else
MsgBox "沒有選中圖片"
End If
End Sub
Private Sub Command2_Click()
Adodc1.Refresh
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("id") = i
Adodc1.Recordset.Fields("qq") = cc
i = i + 1
Adodc1.Recordset.Update
Adodc1.Refresh
End Sub
Private Sub Command3_Click()
Adodc1.Refresh
Dim P() As Byte
Adodc1.RecordSource = "select qq from 表一 where id='" & Text1.Text & "'"
P = Adodc1.Recordset.Fields("qq") '當把一組二進制數據賦值給一個二進制數組時就不用指定數組的大小了,因為是全部賦值
Open App.Path & "\oo.bmp" For Binary As #1
Put #1, , P
Close
End Sub
Private Sub Command4_Click()
Open App.Path & "\oo.bmp" For Binary As #1
Put #1, , cc
Close
End Sub
Private Sub Form_Load()
i = 1
End Sub
D. 如何將圖片插入到資料庫中
第一步://獲取當前選擇的圖片 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;
E. SQL資料庫如何在表裡插入圖片
圖片保存在資料庫中有兩種方式:
1.看一下欄位的欄位類型,如果是二進制(image)的就要將圖片進行二進制轉換;
2.如果是字元串型(nvarchar)的,則直接保存圖片路徑,然後將圖片拷到那個路徑下即可.
一般圖片都是只存路徑的,存二進制占資料庫空間,會導致資料庫過大
F. 怎樣往SQL資料庫中插入圖片,最好舉例說明
首先存儲圖片主要是要保存到一個表內的欄位里。要確定保存的欄位類型為二進制數組等圖片可用的類型,
然後一般的sql工具都能把圖片變成二進制序列。到時候直接存入的時候存成
2進制
數列就可以。
等取出的時候用二進制流取出然後做成跟文件,然後拼接上原來存入文件的
擴展名
就是你剛才存入的文件。