Using system. Data. sqlclient;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{
Try
{
String imgpath = fileupload1.postedfile. filename;
String imgname = imgpath. substring (imgpath. lastindexof ("//") + 1 );
String imgextend = imgpath. substring (imgpath. lastindexof (".") + 1 );
If (! (Imgextend = "BMP" | imgextend = "jpg" | imgextend = "GIF "))
{
Label3.text = "the format of the uploaded image is incorrect! ";
Return;
}
Int filelen = This. fileupload1.postedfile. contentlength;
Byte [] filedata = new byte [filelen];
Httppostedfile HP = fileupload1.postedfile; // create an object for accessing the File Uploaded by the client
Stream sr = hp. inputstream; // create a data stream object
Sr. read (filedata, 0, filelen); // put the image data in the filedata array object instance. 0 indicates the starting position of the array pointer, filelen indicates the length of the stream to be read (the Ending position of the pointer)
Sqlconnection con = new sqlconnection ("Server = (local); User ID = sa; Pwd =; database = ");
Con. open ();
Sqlcommand COM = new sqlcommand ("insert into tb_15 (name) values (@ imgdata)", con );
Com. Parameters. Add ("@ imgdata", sqldbtype. Image );
Com. Parameters ["@ imgdata"]. value = filedata;
Com. executenonquery ();
Label1.text = "saved successfully! ";
}
Catch (Exception error)
{
label1.text = "processing error! Cause: "+ error. tostring ();
}< BR >}< br>
}