Export a picture from the Image Type field in SQL

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;using System.IO;using System.data.sqlclient;using system.data;using system.drawing.imaging;using System.drawing;namespace get name {class program {static void Main (string[] args) {string path = @ "C:\Users\Chinaese\Desktop\_FabricI            MG ";            string x = Path.getfilename (Path.getdirectoryname (Path));            DirectoryInfo root = new DirectoryInfo (path); Directoryinfo[] Childdirs = root.            GetDirectories (); Fileinfo[] files = root.            GetFiles ();            list<string> filename = new list<string> (); foreach (FileInfo item in files) {filename. ADD (Path.getfilenamewithoutextension (item.            FullName)); } string [email protected] "Data Source = 192.168.0.180; Initial Catalog = PMS; User Id = sa;            Password = hualong2012; "; using (SqlConnection connection = new SQLConnection (connectionString)) {SqlCommand command = new SqlCommand ("Select Flowercod E,picimg from Flowerpics where Flowerid in (SELECT MAX (Flowerid) as Expr1 from Flowerpics GROUP by flowercod              e) ", connection); Connection.               Open (); SqlDataReader reader = command.               ExecuteReader ();              Determines whether the data is read to the tail. while (reader.                 Read ()) {//Declaration array, used to hold the database's binary data byte[] MyByte = null; The read data is saved to the array in the try {if (filename.                     Contains (Reader[0])) {continue;                     } MyByte = (byte[]) reader["picimg"];                     Image Image;                     Reads the array data into a file stream MemoryStream Mymemorystream = new MemoryStream (MyByte);                     Convert to a picture format. Image = Image.fromstream (Mymemorystream,true); Image.                     Save (Mymemorystream, imageformat.jpeg); Image. Save (@ "C:\Users\Chinaese\Desktop\SAMP\" + reader[0].                     ToString () + ". bmp", SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP); Mymemorystream.  Close (); Closes the stream filename. ADD (Reader[0].                 ToString ());                 } catch (Exception) {//break; } finally {}//console.writeline (String.Format ("{0}, {1}"              , Reader[0], reader[1]);              }//Be sure to close the reader object. Reader.             Close ();        } console.readkey (); }    }}

  

Export a picture from the Image Type field in 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.