C # Accessing images in SQL Image example

Source: Internet
Author: User

This article mainly introduces C # in SQL Access images Image example, the need for friends can refer to the following

(1) Console application demo Insert Picture

Copy CodeThe code is as follows: public void insertimg () {//reads the picture that needs to be stored as a data stream FileStream fs = new FileStream (@ "E:\c.jpg", FileMode.Open, FileAccess.Read); byte[] Btye2 = new Byte[fs. Length]; Fs. Read (btye2, 0, Convert.ToInt32 (fs. Length)); Fs. Close ();

using (SqlConnection conn = new SqlConnection (SQLCONNSTR)) {Conn. Open (); SqlCommand cmd = new SqlCommand (); Cmd. Connection = conn; Cmd.commandtext = "INSERT into t_img (imgfile) VALUES (@imgfile)"; SqlParameter par = new SqlParameter ("@imgfile", sqldbtype.image); Par. Value = BT; Cmd. Parameters.Add (PAR);

int t= (int) (CMD. ExecuteNonQuery ()); if (T > 0) {Console.WriteLine ("Insert Succeeded");} conn. Close (); } }

(2) The console application reads out and generates the picture to the physical location

Copy CodeThe code is as follows: public void Read () {byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection (sqlconnstr)) {CONN.O Pen (); SqlCommand cmd = new SqlCommand (); Cmd. Connection = conn; Cmd.commandtext = "SELECT * from T_img"; SqlDataReader SDR = cmd. ExecuteReader (); Sdr. Read (); MyData = (byte[]) sdr["Imgfile"];//reads the bit stream of the first picture int arraysize= mydata.getupperbound (0);//Gets the upper limit of the dimension of the bit stream array stored in the database, as the upper bound of the read stream

FileStream fs = new FileStream (@ "C:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write); Fs. Write (MyData, 0, ArraySize); Fs.   Close (); --Write to C:\00.jpg. Conn. Close (); Console.WriteLine ("Read Success");//view files on hard disk}}

(3) The Web picshow.aspx page reads the picture and writes it to the browser to render

Copy CodeThe code is as follows: public void Read () {byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection (sqlconnstr)) {CONN.O Pen (); SqlCommand cmd = new SqlCommand (); Cmd. Connection = conn; Cmd.commandtext = "SELECT * from T_img"; SqlDataReader SDR = cmd. ExecuteReader (); Sdr. Read (); MyData = (byte[]) sdr["Imgfile"]; Response.ContentType = "Image/gif"; Response.BinaryWrite (MyData); Conn. Close (); Response.Write ("read Success"); }

(4) In the Web, you can read and display the picture as on the Picshow.aspx page, while the image is actually referenced in the following example

Copy CodeThe code is as follows:

(5) The method of writing a picture to the SQL database image Type field under WinForm is basically the same as above, except that it can use multiple dialog boxes to help select the storage picture, etc., and the properties can be conveniently used

(6) WinForm read the picture in the PictureBox control display

Method one: Using MemoryStream and System.Drawing.Image

Copy CodeThe code is as follows: public void Read () {byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection (sqlconnstr)) {CONN.O Pen (); SqlCommand cmd = new SqlCommand (); Cmd. Connection = conn; Cmd.commandtext = "SELECT * from T_img"; SqlDataReader SDR = cmd. ExecuteReader (); Sdr. Read (); MyData = (byte[]) sdr["Imgfile"];

MemoryStream mystream = new MemoryStream (MyData); Creates an image image with the specified data stream System.Drawing.Image img = System.Drawing.Image.FromStream (MyStream, true);

System.Windows.Forms.PictureBox Picbox = new PictureBox (); Picbox. Image = img; Picbox. left = 30; Picbox. Top = 80; Picbox. Width = 800; Picbox. Height = 500; This. Controls.Add (Picbox);

MyStream. Close (); Conn. Close (); } }

Method Two: Read the stream directly into the picture and write it to the physical location, and then use the picture to render it.

Copy CodeThe code is as follows: void Read () {using (SqlConnection conn = new SqlConnection (SQLCONNSTR)) {Conn. Open (); SqlCommand cmd = new SqlCommand (); Cmd. Connection = conn; Cmd.commandtext = "SELECT * from T_img"; SqlDataReader SDR = cmd. ExecuteReader (); Sdr. Read ();

Byte[] image_img = (byte[]) sdr["Imgfile"]; if (image_img. Length = = 0) {return;} int filelength = image_img. Length; String imageName = "1.jpg"; String myurl = environment.currentdirectory + "\ \" + ImageName; FileStream fs = new FileStream (Myurl, filemode.openorcreate,fileaccess.write); BinaryWriter BW = new BinaryWriter (FS); Bw. Basestream.write (image_img, 0, filelength); Bw. Flush (); Bw. Close (); System.Windows.Forms.PictureBox Picbox = new PictureBox ();

Add a//picbox image method to Picbox. ImageLocation = Myurl; Picbox. Width = 800; Picbox. Height = 300;

Add a picture method for Picbox two Bitmap Bitmap = new Bitmap (Myurl); Picbox. Width = 100;//bitmap. Width; Picbox. Height = 80;//bitmap. Height; Picbox. Image = (image) bitmap; Picbox. SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; Picbox. left = 20; Picbox. Top = 30;

This. Controls.Add (Picbox); Conn. Close ();

} }

C # Accessing images in SQL Image example

Related Article

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.