Upload images to SQL Server

Source: Internet
Author: User
Upload an image to SQL Server and save it as an image

Protected void button#click (Object sender, eventargs E)
{
Httppostedfile post = imginput. postedfile;
Int lent = post. contentlength;
Byte [] lentimg = new byte [lent];
Stream photostream = post. inputstream;
Photostream. Read (lentimg, 0, lent );
Sqlcommand sqlcom = new sqlcommand ("insert into zhaoxiaomingpic values (@ IMG)", con );
Sqlcom. Parameters. Add ("@ IMG", sqldbtype. Image );
Sqlcom. Parameters ["@ IMG"]. value = lentimg;
Con. open ();
Sqlcom. executenonquery ();
Con. Close ();
}

Read data from an image-type table.

// Web Part

String STR = configurationsettings. deleettings ["connstring"]. tostring ();
Sqlconnection con = new sqlconnection (STR );
String SQL = "select personimage, personimagetype from person where personemail = '" + session ["email"] + "'";
Sqlcommand cmd = new sqlcommand (SQL, con );
Con. open ();
Sqldatareader DR = cmd. executereader ();
Byte [] picturedata = NULL;
While (dr. Read ())
{
Picturedata = (byte []) Dr [0];
Response. contenttype = Dr [1]. tostring ();
}
This. enableviewstate = false;
Response. appendheader ("contect-length", picturedata. length. tostring ());
Response. binarywrite (picturedata );
Response. End ();

//CodeWrite a show. ASP page,

// Load an image control on the called page,

// Call the code: This. image1.imageurl = "show. aspx ";

// Winfrom

String strcon = "Data Source = AAAA; initial catalog = school; uid = sa; Pwd = ";
Sqlconnection con = new sqlconnection (strcon );
Con. open ();
String SQL = "select personimage from person where personid =" + this. textbox1.text + "";
Sqlcommand cmd = new sqlcommand (SQL, con );
Sqldatareader DR = cmd. executereader ();
If (dr. hasrows)
{
Dr. Read ();
Byte [] IMG = (byte []) Dr ["personimage"];
Memorystream MS = new memorystream ();
Ms. Write (IMG, 0, IMG. Length );
This. picturebox1.image = image. fromstream (MS );
Ms. Close ();
}
Dr. Close ();

 

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.