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