李閩
摘要:文章介紹了在ASP.NET環(huán)境下,對于SQL server數(shù)據(jù)庫,存儲(chǔ)大型二進(jìn)制對象的方法。通過HttpPostedfile類的InputStream屬性獲得Stream對象,用Stream對象讀取欲上傳文件內(nèi)容。建立數(shù)據(jù)庫連接,將文件存入數(shù)據(jù)庫的二進(jìn)制數(shù)據(jù)類型中。
關(guān)鍵詞:二進(jìn)制文件;數(shù)據(jù)庫;HttpPostedfile類;Stream對象;上傳
中圖分類號(hào):TP311文獻(xiàn)標(biāo)識(shí)碼:A文章編號(hào):1009-3044(2012)18-4410-02
Binary Files in the Database Storage
LI Min
(Suzhou Art, Design Technology Institute, Suzhou 215000, China )
Abstract: This paper introduces the ASP.NET environment in SQL server database to store large binary objects method. By HttpPosted file class InputStream property to a Stream object, use the Stream object to read to upload the contents of the file. Establish a database con nection, the binary data type of the file stored in the database.
Key words: binary files; database; class HttpPostedfileL; object StreamL; upload
上傳到網(wǎng)站的文件有多種形式,有圖形圖像文件、聲音文件、視頻文件、文該文件等。這些文件基本上是保存在數(shù)據(jù)庫中。文件在數(shù)據(jù)庫中的存儲(chǔ)主要有兩種方式,一種是將文件的路徑保存到數(shù)據(jù)庫,文件保存到預(yù)先指定的位置。另一種是直接將文件以二進(jìn)制形式保存到數(shù)據(jù)庫中。該文主要介紹第二種保存方式。
在asp.net環(huán)境下,將二進(jìn)制數(shù)據(jù)保存到數(shù)據(jù)庫中,要應(yīng)用到HttpPostedFile類的InputStream屬性、Stream類的Read方法和Byte數(shù)據(jù)類型。
對于一個(gè)要上傳的文件,用HttpPostedFile類的InputStream屬性獲取一個(gè)Stream對象,該對象就指向這個(gè)上傳文件,可以用來讀取文件的內(nèi)容。
HttpPostFile hp=FileUpload1.PostedFile;//創(chuàng)建訪問上傳文件的對象
Stream sr=hp.InputStream;//創(chuàng)建Stream對象
從Stream對象中用Read方法讀取相應(yīng)的數(shù)據(jù)。
public abstract int Read(
byte[] buffer,
int offset,
int count
)
Read()方法可以把特定數(shù)量的字節(jié)讀入到一個(gè)數(shù)組中(就是第一個(gè)參數(shù)buffer),返回實(shí)際讀取的字節(jié)數(shù)。如果值為0,表示到了流的末尾。第二個(gè)參數(shù)是一個(gè)偏移量,用它可以要求Read操作的數(shù)據(jù)是從數(shù)組的哪個(gè)元素開始填充,而不一定是從第一個(gè)元素開始。第三個(gè)參數(shù)是要讀入數(shù)組的字節(jié)數(shù)。代碼如下。
HttpFileCollection MyFileCollection;
HttpPostedFile MyFile;
int FileLen;
MyFileCollection = Request.Files;
MyFile = MyFileCollection[0];
FileLen = MyFile.ContentLength;
byte[] input = new byte[FileLen];//建立byte數(shù)組
MyStream = MyFile.InputStream;//初始化stream.
MyStream.Read(input, 0, FileLen);//讀文件到byte數(shù)組
for (int Loop1 = 0; Loop1 < FileLen; Loop1++)
MyString = MyString + input[Loop1].ToString();
//將數(shù)據(jù)從數(shù)組copy到字符串中
把二進(jìn)制文件往數(shù)據(jù)庫存儲(chǔ)時(shí),要先建立數(shù)據(jù)庫連接,以SQL Server為例,
String Constr="server=dataserver;user id =sa;pwd=;database=db_ADO";
SqlConnection con=new SqlConnection(Constr);
string SqlStr="select *from mydata";
SqlDataAdapter ada=new SqlDataAdapter (SqlStr,con);
con.Open(); //打開數(shù)據(jù)庫連接
SQL Server存儲(chǔ)二進(jìn)制數(shù)據(jù)使用的數(shù)據(jù)類型有binary、varbinary、image,用byte類型的數(shù)組作為二進(jìn)制參數(shù)的傳遞。主要代碼如下:
If(this.FileUpload1.PostedFile.FileName!="")
{
string ImgPath=FileUpload1.PostedFile.FileName;
string ImgName=ImgPath.Substring(ImgPath.LastIndexOf("\")+1);
string ImgExtend=ImgPath.Substring(ImgPath.LastIndexOf(".")+1);
FileLen=this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData=new Byte[FileLen];
HttpPostedFile hp=FileUpload1.PostedFile;
Stream st=hp.InputStream;
sr.Read(FileData,0,FileLen);
String Constr="server=dataserver;user id =sa;pwd=;database=db_ADO";
SqlConnection con=new SqlConnection(Constr);
string SqlStr="select *from mydata";
SqlDataAdapter ada=new SqlDataAdapter (SqlStr,con);
con.Open();
SqlCommand com=new SqlCommand("INSERT INTO tb_fileUp(name) VALUES(@imgdata)",con);
com.Parameters.Add("@imgdata",SqlDbType.Image);
com.Parameters["@imgdata"].Value=Filedata;
com.ExecuteNonQuery();
}
參考文獻(xiàn):
[1]劉麗霞.零基礎(chǔ)學(xué)C# 3.0[M].北京:機(jī)械工業(yè)出版社,2009.
[2]鄭阿奇.ASP.NET 3.5實(shí)用教程[M].北京:電子工業(yè)出版社,2009.
[3]吳戈.SQL Server 2008學(xué)習(xí)筆記[M].北京:人民郵電出版社,2009.