Net9: Image file converted to binary stream into SQL database and read binary stream output file from database

Source: Internet
Author: User

The original Published time: 2008-08-10--from my Baidu article [imported by moving tools]

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;


Using System.Data.SqlClient;

public partial class Default4:System.Web.UI.Page
{
SqlConnection conn = new SqlConnection (configurationmanager.connectionstrings["Imgdataconn"]. ConnectionString);
protected void Page_Load (object sender, EventArgs e)
{

   }
    protected void Button1_Click (object sender, EventArgs e)
    {
         Guid gid = Guid.NewGuid ();
        Conn. Open ();
        SqlCommand cmd = new SqlCommand ("Insert into Imgdata (gid,filedata) VALUES (@gid, @filedata) ", conn);
        cmd. Parameters.Add ("@gid", Sqldbtype.uniqueidentifier). Value = GID;
        cmd. Parameters.Add ("@filedata", Sqldbtype.image). Value=fileupload1.filebytes;
        cmd. ExecuteNonQuery ();
        Conn. Close ();

session["GID"]=gid;
}
protected void button2_click (object sender, EventArgs e)
{
Conn. Open ();
SqlCommand cmd = new SqlCommand ("Select Filedata from Imgdata where gid= '" + session["gid"]. ToString () + "'", conn);
Byte[] FBT = (byte[]) cmd. ExecuteScalar ();
Conn. Close ();
Response.OutputStream.Write (FBT, 0, FBT. Length);
Response.End ();
}
}

Net9: Image file converted to binary stream into SQL database and read binary stream output file from database

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.