Asp. NET saves files in binary form to SQL Server database

Source: Internet
Author: User

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! "; }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.