When you develop a website, you often need to have the ability to upload files in the site, there are two ways to upload files: Save the file name to the database, save the file to the server location, and save the file directly to the database
This method describes saving files in binary form to the database (you can save Word documents, Notepad text, pictures, compressed packages ...). )
if( This. FileUpload1.PostedFile.FileName! ="") { stringImgpath =FileUpload1.PostedFile.FileName; stringImgname = imgpath.substring (Imgpath.lastindexof ("\\") +1); stringImgextend = imgpath.substring (Imgpath.lastindexof (".") +1); intFileLen = This. FileUpload1.PostedFile.ContentLength; Byte[] FileData=NewByte[filelen]; Httppostedfile HP=Fileupload1.postedfile; Stream SR=HP. InputStream; Sr. Read (FileData,0, FileLen); SqlConnection Con=NewSqlConnection ("server=.; User id=sa;pwd=;d atabase=db_09"); Con. Open (); SqlCommand com=NewSqlCommand ("INSERT into Tb_fileup (name) VALUES (@imgdata)", con); Com. Parameters.Add ("@imgdata", Sqldbtype.image); Com. parameters["@imgdata"]. Value =FileData; Com. ExecuteNonQuery (); This. Lblmessage.text ="Save success! "; }