c# - Image upload to database -


so managed set-up simple test page upload , store image database unsure of whether storage successful.

whenever store image table under column image datatype image, in new row <binary data>. <binary data> image?

i expecting display image in '0' , '1' can compare different items stored. having "" stored means image had been stored?

my website's logic coded in c#.

and had been trying find sources examples how may retrieve image display.

this current insert statement

sqlcommand com = new sqlcommand("insert imagetotable "     + "(myphoto,name) values (@photo, @name)", con); 

to retrieve data work?

sqlcommand com2 = new sqlcommand("select * imagetotable userid ='1'", con); 

so if use datareader store selected items, can store image display, label, image button, etc?

and how store image variables? example if want store text use:

pw = dr["password"].tostring();**   

therefore images like?

edit: full button on click event handle image strage

    protected void button1_click(object sender, eventargs e) {     sqlconnection con = new sqlconnection(@"data source=*;initial catalog=*;integrated security=true");     if (!fileupload1.hasfile)     {         label1.visible = true;         label1.text = "please select image file";    //checking if file uploader has no file selected       }     else     {         int length = fileupload1.postedfile.contentlength;         byte[] pic = new byte[length];           fileupload1.postedfile.inputstream.read(pic, 0, length);          try         {               con.open();             sqlcommand com = new sqlcommand("insert imagetotable "               + "(myphoto,name) values (@photo, @name)", con);             com.parameters.addwithvalue("@photo", pic);             com.parameters.addwithvalue("@name", textbox1.text);             com.executenonquery();             label1.visible = true;             label1.text = "image uploaded sucessfully";  //after sucessfully uploaded image          }                 {              con.close();         }      } } 

first, image type in db maps byte[] in c#, should convert image byte[] before inserting database. retrieving image database, can use code:

 memorystream stream = new memorystream(byte[]);// can read image dataadapter or datareader  system.drawing.image img = system.drawing.image.fromstream(stream); 

here link: http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html , hope can you.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -