The database stores pictures and reads pictures

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Data.SqlClient;namespacewindowsformsapplication2{ Public Partial classForm2:form { PublicForm2 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {OpenFileDialog op=NewOpenFileDialog (); DialogResult isOK=op.            ShowDialog (); if(isok==DialogResult.OK) {//exposes a file-based Stream that supports both synchronous read and write operations, as well as asynchronous read and write operations. FileStream fs =NewFileStream (Op. FileName, FileMode.Open);//mode is a constant used to determine how to open or create a file. Image img =System.Drawing.Bitmap.FromStream (FS); pictureBox1.Image=img; Fs.            Close (); }        }        Private voidButton2_Click (Objectsender, EventArgs e) {OpenFileDialog op=NewOpenFileDialog (); DialogResult isOK=op.            ShowDialog (); if(isOK = =DialogResult.OK) {//reading pictures into the databaseFileStream fs =NewFileStream (Op.                FileName, FileMode.Open); BinaryReader BR=NewBinaryReader (FS);//Binary Reader                byte[] buffer=New byte[FS. Length];//Binary byte arrayBuffer=br. Readbytes ((int) fs.                Length); SqlConnection Conn=NewSqlConnection ("server=.; Database=car;uid=sa;pwd=123"); SqlCommand cmd=Conn.                CreateCommand (); Cmd.commandtext="insert INTO PIC values (4, @buffer)"; Cmd.                Parameters.clear (); Cmd. Parameters.Add ("@buffer", buffer); Conn.                Open (); Cmd.                ExecuteNonQuery (); Cmd.                Dispose (); Cmd.commandtext="Select top 1 * from pic ORDER BY code DESC"; SqlDataReader Dr=cmd.                ExecuteReader (); byte[] by =New byte[FS.                Length]; if(Dr. Read ()) { by=(byte[]) (dr["Pics"]); } MemoryStream Ms=NewMemoryStream (by); Image img=System.Drawing.Bitmap.FromStream (MS); pictureBox1.Image=img; }        }    }}

The database stores pictures and reads pictures

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.