Upload a picture between a database and a form

Source: Internet
Author: User

 Private voidButton1_Click (Objectsender, EventArgs e)//open pictures from hard disk appear inside the form{OpenFileDialog op=NewOpenFileDialog ();//Create an open dialog boxDialogResult Dr =op. ShowDialog ();//The Open dialog box is displayed.            if(Dr==dialogresult.ok)//If the point is "open"{FileStream fs=NewFileStream (Op. Filename,filemode.open);//get the path to your open picture and save it inside the FS stream.//MessageBox.Show (Op. FileName);Image img = System.Drawing.Bitmap.FromStream (FS);//draw fs out through the drawing functionpictureBox1.Image = img;//Picture contents of picture frame            }        }

///////////////

 //Here's a picture of opening a photo to the database.        Private voidButton2_Click (Objectsender, EventArgs e)//open a picture and save it in the database.{OpenFileDialog op=NewOpenFileDialog (); DialogResult Dr=op.            ShowDialog (); if(dr = =DialogResult.OK) {FileStream fs=NewFileStream (Op. Filename,filemode.open);//get the path to your open picture and save it inside FS//The following begins to convert the path to bytes into the database.BinaryReader br =NewBinaryReader (FS);//Convert FS (stream that holds picture path) to binary deposit into BR inside go binaryreader binary reader//int[] x= new int[10];                byte[] butter=New byte[FS. Length];//create an array of byte typesButter = br. Readbytes ((int) fs. Length);//Assigns a value to the byte butter, which is the stream converted to binary data//start connecting to the database below and then save the picture inside.SqlConnection conn =NewSqlConnection ("server=.; Database=cml;user=sa;pwd=123"); SqlCommand cmd=Conn.                CreateCommand (); Conn.                Open (); Cmd.commandtext="insert into LK values (@s)"; Cmd.                Parameters.clear (); Cmd. Parameters.Add ("@s", butter);//butter Open a picture's path → binary data → bytescmd.                ExecuteNonQuery (); Cmd.                Dispose (); Conn.            Close (); }        }

/////////////////////

 Private voidButton3_Click (Objectsender, EventArgs e)//to display a picture on a form from a database{SqlConnection conn=NewSqlConnection ("server=.; Database=cml;user=sa;pwd=123"); SqlCommand cmd=Conn.            CreateCommand (); Conn.            Open (); Cmd.commandtext="select top 1* from LK order by IDs Desc";//ORDER by sort GROUP by groupSqlDataReader dr =cmd.            ExecuteReader (); Dr. Read ();//Start reading Data                        byte[] butter = (byte[]) dr["img"]; //butter is now a byte, but the image type inside the form is image, so to go back MemoryStream is the memory streamMemoryStream ms =NewMemoryStream (butter);//has converted the butter into a stream.Image img = System.Drawing.Bitmap.FromStream (ms);//draw a stream into a picturepictureBox1.Image =img; }

Transfer pictures between databases and forms

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.