Upload files to the SQL Server database code in ASP. NET2.0

Source: Internet
Author: User

SQL Server database code

 

&amp; Lt; % @ Page Language = &amp; quot; C # &amp; quot; EnableViewState = &amp; quot; true &amp; quot; % &amp; gt; &amp; lt; % @ Import Namespace = &amp; quot; system. data. sqlClient &amp; quot; % &amp; gt; &amp; lt ;! DOCTYPE html PUBLIC &amp; quot;-// W3C // dtd xhtml 1.0 Transitional // EN &amp; quot; http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;script Runat = &amp; quot; server &amp; quot; &amp; gt; string strCnn = &amp; quot; Persist Security Info = False; User ID = sa; Password =; Initial Catalog = Book; server = (local); &amp; quot; protected void button#click (object sender, EventArgs e) {System. IO. stream fileDataStream = FileUpload1.PostedFile. inputStream; if (fileDataStream. length &amp; lt; 1) {Msg. text = &amp; quot; select a file. &amp; Quot; return ;}// get the file size int fileLength = FileUpload1.PostedFile. contentLength; // create an array byte [] fileData = new byte [fileLength]; // fill in the file stream to the array fileDataStream. read (fileData, 0, fileLength); // obtain the file type string fileType = FileUpload1.PostedFile. contentType; // construct a database connection, SQL statement, and create the SqlConnection myConnection = new SqlConnection (strCnn) parameter; SqlCommand command = new SqlCommand (&amp; quot; INSERT INTO UserPhoto (UserName, ContentType, P Hoto) &amp; quot; + &amp; quot; VALUES (@ UserName, @ ContentType, @ Photo) &amp; quot;, myConnection); command. parameters. addWithValue (&amp; quot; @ UserName &amp; quot;, TextBox1.Text); command. parameters. addWithValue (&amp; quot; @ ContentType &amp; quot;, fileType); command. parameters. addWithValue (&amp; quot; @ Photo &amp; quot;, fileData); // open the connection and run the query myConnection. open (); command. executeNonQuery (); myConnection. close (); Response. redirect (Request. rawUrl);} protecte D void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {BindGrid () ;}} private void BindGrid () {SqlConnection myConnection = new SqlConnection (strCnn); SqlCommand myCommand = new SqlCommand (&amp; quot; SELECT * FROM UserPhoto Order By id DESC &amp; quot;, myConnection); try {myConnection. open (); GridView1.DataSource = myCommand. executeReader (System. data. commandBehavior. closeConnection); GridView1.DataBind ();} catch (Exception SQLexc) {Response. write (&amp; quot; extract An error occurred while retrieving data: &amp; quot; + SQLexc. ToString () ;}} protected string FormatURL (object strArgument) {return &amp; quot; ReadImage. aspx? Id = &amp; quot; + strArgument. ToString () ;}&amp; lt;/script &amp; gt; &amp; lt; html xmlns = &amp; quot; http://www.w3.org/1999/xhtml"&gt;<pead Id = &amp; quot; Head1 &amp; quot; runat = &amp; quot; server &amp; quot; &amp; gt; &amp; lt; title &amp; gt; upload files to the database &amp; lt;/title &amp; gt; &amp; lt;/head &amp; gt; &amp; lt; body &amp; gt; &amp; lt; form id = &amp; quot; MengXianhui &amp; quot; runat = &amp; quot; server &amp; quot; &amp; gt; &amp; lt; asp: GridView ID = &amp; quot; GridView1 &amp; quot; runat = &amp; quot; server &amp; quot; AutoGenerateColumns = &amp; quot; false &amp; quot; &amp; gt; &amp; lt; columns &amp; gt; &amp; lt; asp: TemplateField &amp; gt; &amp; lt; ItemTemplate &amp; gt; &amp; lt; % # Eval (&amp; quot; UserName &amp; quot;) % &amp; gt; &amp; lt;/ItemTemplate &amp; gt; &amp; lt;/asp: TemplateField &amp; gt; &amp; lt; ItemTemplate &amp; gt; &amp; lt; img src = &amp; quot; &amp; lt; % # FormatURL (Eval (&amp; quot; id &amp; quot;) % &amp; gt; &amp; quot;/&amp; gt; &amp; lt; /ItemTemplate &amp; gt; &amp; lt;/asp: TemplateField &amp; gt; &amp; lt;/Columns &amp; gt; &amp; lt;/asp: GridView &amp; gt; &amp; lt; br/&amp; gt; &amp; lt; br/&amp; gt; Name: &amp; lt; asp: TextBox ID = &amp; quot; TextBox1 &amp; quot; runat = &amp; quot; server &amp; quot; &amp; gt; &amp; lt;/asp: TextBox &amp; gt; &amp; lt; br/&amp; gt; Photo: &amp; lt; asp: FileUpload ID = &amp; quot; FileUpload1 &amp; quot; runat = &amp; quot; server &amp; quot;/&amp; gt; &amp; lt; asp: Button ID = &amp; quot; btnUpload &amp; quot; runat = &amp; quot; server &amp; quot; text = &amp; quot; upload &amp; quot; OnClick = &amp; quot; button#click &amp; quot; &amp; gt; &amp; lt;/asp: Button &amp; gt; &amp; lt; p &amp; gt; &amp; lt; asp: Label ID = &amp; quot; Msg &amp; quot; runat = &amp; quot; server &amp; quot; ForeColor = &amp; quot; Red &amp; quot; &amp; gt; &amp; lt; /asp: Label &amp; gt; &amp; lt;/p &amp; gt; &amp; lt;/form &amp; gt; &amp; lt;/body &amp; gt; &amp; lt;/html &amp; gt;

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.