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
}
}