WinForm picture read stored in database SQL

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Data.SqlClient;namespacewindowsformsapplication10{ Public Partial classForm2:form { PublicForm2 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {Openfiledialog1.filter="@. jpg|*.jpg|@. gif|*.gif|@. Png|*.png| All Files|*.*"; DialogResult Dr=Openfiledialog1.showdialog (); if(dr = =DialogResult.OK) {//to read a picture into a file streamFileStream fs =NewFileStream (Openfiledialog1.filename,filemode.open,fileaccess.read);//PathImage img = System.Drawing.Bitmap.FromStream (FS);//draw, flow into picturespictureBox1.Image = img;//Specify, display pictureFS.                          Close (); }        }        Private voidButton2_Click (Objectsender, EventArgs e)//Deposit Database{Openfiledialog1.filter="@. jpg|*.jpg|@. gif|*.gif|@. Png|*.png| All Files|*.*"; DialogResult Dr=Openfiledialog1.showdialog (); if(dr = =DialogResult.OK) {//file StreamFileStream fs =NewFileStream (Openfiledialog1.filename,filemode.open,fileaccess.read); BinaryReader BR=NewBinaryReader (FS);//Binary Reader                byte[] buffer = br. Readbytes (int. Parse (fs.                Length.tostring ())); //connecting to a databaseSqlConnection conn =NewSqlConnection ("server=.; Database=snewdata;user=sa;pwd="); SqlCommand cmd=Conn.                CreateCommand (); Cmd.commandtext="INSERT into imgtable values (@buffer)"; Cmd. Parameters.Add ("@buffer", buffer); Conn.                Open (); Cmd.                ExecuteNonQuery (); Cmd.                Dispose (); Conn.            Close (); }        }        Private voidButton3_Click (Objectsender, EventArgs e)//Database Read        {            //Read Database            byte[] buffer =NULL; SqlConnection Conn=NewSqlConnection ("server=.; Database=snewdata;user=sa;pwd="); SqlCommand cmd=Conn.            CreateCommand (); Cmd.commandtext="Select *from imgtable where code=4"; Conn.            Open (); SqlDataReader Dr=cmd.            ExecuteReader (); if(Dr. Read ()) {buffer= (byte[]) dr["IMGs"]; } cmd.            Dispose (); Conn.            Close (); //display the binary data buffer as a pictureMemoryStream ms =NewMemoryStream (buffer);//Building ObjectsMs. Write (Buffer,0, buffer. Length);//write to the memory streamImage img =System.Drawing.Image.FromStream (MS); pictureBox1.Image=img; }    }}

WinForm picture read stored in database SQL

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.