Database format:
ID int
Imgtype varchar (50)
IMG Image
Save Picture:
Upload image protected void Button1_Click (object sender, EventArgs e) {//upload control httppostedfile HPF = fileupload1.postedfile;// Set a cache byte data (binary code for the user to hold the picture) byte[] b = new BYTE[HPF. contentlength];//gets the stream of the uploaded picture stream s = hpf. inputstream;//the stream into the cache s. Read (b, 0, HPF. CONTENTLENGTH);//want to add data to the database string sql = "INSERT into Imginfo (imgtype,img) VALUES (@imgtype, @img)"; con. Open (); SqlCommand com = new SqlCommand (sql, con);//Add a picture of binary code COM. Parameters.Add ("@img", Sqldbtype.binary, HPF. ContentLength). Value = b;//Adds a picture of type com. Parameters.Add ("@imgtype", SqlDbType.Char, 50). Value = HPF. Contenttype;com. ExecuteNonQuery (); con. Close ();}
Take pictures:
SqlDataAdapter da = new SqlDataAdapter ("select * from Imginfo", con);D ataset ds = new DataSet ();d A. Fill (DS);//Set output stream type Response.ContentType = ds. Tables[0]. ROWS[0][1]. ToString (); byte[] B = (byte[]) ds. Tables[0]. rows[0][2];//output Picture response.binarywrite (b);//Response.Write (ds. Tables[0]. ROWS[0][2]. ToString ());
This article is from the "Program Ape's Home" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1410805