詳解圖片上傳到數(shù)據(jù)庫(kù)
下面我來匯總一下如何將圖片保存到SqlServer、Oracle、Access數(shù)據(jù)庫(kù)中,下文內(nèi)容供大家參考學(xué)習(xí),希望對(duì)大家能夠有所幫助。
首先,我們要明白圖片是以二進(jìn)制的形式保存在數(shù)據(jù)庫(kù)中的,那么把圖片保存到數(shù)據(jù)庫(kù)中的步驟大體上有這幾步
1.將圖片轉(zhuǎn)換為二進(jìn)制數(shù)組(byte[]);
2.把轉(zhuǎn)換后的二進(jìn)制數(shù)組(byte[])作為參數(shù)傳遞給要執(zhí)行的Command;
3.執(zhí)行Command;
首先,如何把圖片轉(zhuǎn)換成byte[],如果你使用的是ASP.Net2.0,那么你可以使用FileUpLoad控件來實(shí)現(xiàn)
byte[] fileData = this.FileUpload1.FileBytes;
如果你用的是ASP.Net1.1或者你在創(chuàng)建WinForm那么你可以使用下面的方法來把圖片轉(zhuǎn)換為byte[]
- public byte[] getBytes(string filePath)
- {
- System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
- byte[] imgData = new byte[fs.Length];
- fs.Read(imgData, 0, (int)fs.Length);
- return imgData;
- }
接下來我們要做的就是要把已經(jīng)得到的byte[]作為參數(shù)傳遞給Command對(duì)象
1.SqlServer數(shù)據(jù)庫(kù)。SqlServer有Image字段類型,***可以存儲(chǔ)2G的數(shù)據(jù)。 byte[] fileData = this.FileUpload1.FileBytes;
- string sql = "insert into t_img(img) values (@img)";
- string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["fengdongDB"].ToString();
- SqlConnection sqlConn = new SqlConnection(strconn);
- SqlCommand sqlComm = new SqlCommand(sql, sqlConn);
- sqlComm.Parameters.Add("@img", SqlDbType.Image);//添加參數(shù)
- sqlComm.Parameters["@img"].Value = fileData;//為參數(shù)賦值
- sqlConn.Open();
- sqlComm.ExecuteNonQuery();
- sqlConn.Close();
2.Oracle數(shù)據(jù)庫(kù)。在Oracle數(shù)據(jù)庫(kù)中我們可以使用BLOB字段類型,***可以存儲(chǔ)4G的數(shù)據(jù)。
- byte[] fileData = this.FileUpload1.FileBytes;
- string sql = "insert into t_img(imgid,IMGDATA) values(100,:IMGDATA)";
- string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ToString();
- OracleConnection oraConn = new OracleConnection(strconn);
- OracleCommand oraComm = new OracleCommand(sql, oraConn);
- oraComm.Parameters.Add(":IMGDATA", OracleType.Blob);//添加參數(shù)
- oraComm.Parameters[":IMGDATA"].Value = fileData;//為參數(shù)賦值
- oraConn.Open();
- oraComm.ExecuteNonQuery();
- oraConn.Close();
注意:這里我需要說明一下,用Oracle的專用連接傳遞參數(shù)的時(shí)候你要小心一點(diǎn),看看上面的SQL語句你就會(huì)知道,要在參數(shù)名前加個(gè)“:”否則就會(huì)出現(xiàn)下面的錯(cuò)誤“OracleException: ORA-01036: 非法的變量名/編號(hào)”。這里需要我們注意一下。另外還有一個(gè)地方,當(dāng)我引用System.Data.OracleClient命名空間的時(shí)候默認(rèn)是沒有的,必須添加對(duì)System.Data.OracleClient的引用,我記得在VS2003下如果安裝了OracleClient是不用添加引用就可以引入的。這里也要留意一下。
3.Access數(shù)據(jù)庫(kù)。在Access中我們使用OLE對(duì)象字段類型,***支持1G的數(shù)據(jù)。
- byte[] fileData = this.FileUpload1.FileBytes;
- string sql = "insert into t_img(IMGDATA) values(?)";
- string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForAccess"].ToString();
- OleDbConnection oleConn = new OleDbConnection(strconn);
- OleDbCommand oleComm = new OleDbCommand(sql, oleConn);
- oleComm.Parameters.Add("imgdata", OleDbType.Binary);
- oleComm.Parameters["imgdata"].Value = fileData;
- oleConn.Open();
- oleComm.ExecuteNonQuery();
- oleConn.Close();
好了,到這里我們就把圖片保存到數(shù)據(jù)庫(kù)中全部說完了,接下來要說的是如何從數(shù)據(jù)庫(kù)中把圖片讀取出來。實(shí)際上這是與插入操做相反的一個(gè)過程:先報(bào)把從數(shù)據(jù)庫(kù)中獲取的圖片數(shù)據(jù)轉(zhuǎn)換為數(shù)組,然后把數(shù)組轉(zhuǎn)換為圖片。不同數(shù)據(jù)之間沒有特別大的差異,我這里只列出從Oracle數(shù)據(jù)庫(kù)中把數(shù)據(jù)讀取出來以供參考。
- private byte[] getImageDataFromOracle()
- {
- string sql = "select IMGDATA from t_img where imgID=100";
- string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ToString();
- OracleConnection oraConn = new OracleConnection(strconn);
- OracleCommand oraComm = new OracleCommand(sql, oraConn);
- oraConn.Open();
- byte[] fileData = (byte[])oraComm.ExecuteScalar();
- oraConn.Close();
- return fileData;
- }
我們獲取到了數(shù)據(jù),那么把byte[]轉(zhuǎn)換為圖片的過程都是一樣的。
- private System.Drawing.Image convertByteToImg(byte[] imgData)
- {
- System.IO.MemoryStream ms = new System.IO.MemoryStream(imgData);
- System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
- return img;
如果你在開發(fā)WinForm應(yīng)用的話你可以直接把返回結(jié)果保存或者顯示到PictureBox里,如果你在使用ASP.Net那么你可以在單獨(dú)的一個(gè)頁面把圖片輸出,在另外一個(gè)頁面把Image控件的ImageUrl屬性指向圖片輸出頁面。
比如輸出頁面getImg.aspx的代碼
- protected void Page_Load(object sender, EventArgs e)
- {
- string sql = "select IMGDATA from t_img where imgID=100";
- string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ToString();
- OracleConnection oraConn = new OracleConnection(strconn);
- OracleCommand oraComm = new OracleCommand(sql, oraConn);
- oraConn.Open();
- byte[] fileData = (byte[])oraComm.ExecuteScalar();
- oraConn.Close();
- System.IO.MemoryStream ms = new System.IO.MemoryStream(fileData);
- System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
- img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
【編輯推薦】