Upload the image file to the database and read it from the database.

Source: Internet
Author: User

Upload images to the database:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;

Namespace myupload
{
/// <Summary>
/// Summary of img2sql.
/// </Summary>
Public class img2sql: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox imgtitletextbox;
Protected system. Web. UI. htmlcontrols. htmlinputfile uploadimg;
Protected system. Web. UI. webcontrols. requiredfieldvalidator requiredfieldvalidator1;
Protected system. Web. UI. webcontrols. Button button1;
Protected sqlconnection myconnection;

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
String conn = "Server = (local); database = test; uid = sa; Pwd = ilovenm ";
Myconnection = new sqlconnection (conn );
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void button#click (Object sender, system. eventargs E)
{
Stream mystream = uploadimg. postedfile. inputstream;
Int imgdatalen = uploadimg. postedfile. contentlength;
String imgtype = uploadimg. postedfile. contenttype;
String imgtitle = imgtitletextbox. text;
Byte [] imgdata = new byte [imgdatalen];
Int n = mystream. Read (imgdata, 0, imgdatalen );

// String conn = "Server = (local); database = test; uid = sa; Pwd = ilovenm ";
// Sqlconnection myconnection = new sqlconnection (conn );

Sqlcommand mycommand = new sqlcommand ("insert into image (imgtitle, imgtype, imgdata) values (@ imgtitle, @ imgtype, @ imgdata)", myconnection );

Mycommand. Parameters. Add (New sqlparameter ("@ imgtitle", sqldbtype. varchar, 50 ));
Mycommand. Parameters ["@ imgtitle"]. value = imgtitle;

Mycommand. Parameters. Add (New sqlparameter ("@ imgtype", sqldbtype. varchar, 50 ));
Mycommand. Parameters ["@ imgtype"]. value = imgtype;

Mycommand. Parameters. Add (New sqlparameter ("@ imgdata", sqldbtype. Image ));
Mycommand. Parameters ["@ imgdata"]. value = imgdata;

Myconnection. open ();
Int numrowsaffected = mycommand. executenonquery ();
Myconnection. Close ();
}
}
}

Display:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace myupload
{
/// <Summary>
/// Summary of showimg.
/// </Summary>
Public class showimg: system. Web. UI. Page
{
Protected sqlconnection myconnection;

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
String imgid = request. querystring ["imgid"];
String conn = "Server = (local); database = test; uid = sa; Pwd = ilovenm ";
// String conn = "Server = (local); database = test; uid = sa; Pwd = ilovenm ";
Myconnection = new sqlconnection (conn );

String selectcmd = "select imgdata, imgtype from image where id =" + imgid;
Sqlcommand mycommand = new sqlcommand (selectcmd, myconnection );
Myconnection. open ();
Sqldatareader mydatareader = mycommand. executereader ();

If (mydatareader. Read ())
{
Response. contenttype = mydatareader ["imgtype"]. tostring ();
Response. binarywrite (byte []) mydatareader ["imgdata"]);
}
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}

 

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.