C # pictures stored in SQL Server database

Source: Internet
Author: User

OpenFileDialog OpenFileDialog1 =NewOpenFileDialog (); if(OpenFileDialog1. ShowDialog () = =DialogResult.OK) {TextBox1.Text= OpenFileDialog1. FileName;//file path            }            stringImageName = DateTime.Now.ToString ("YYYYMMDDHHMMSS");//generate file name to databaseFileStream FS=NewFileStream (TextBox1.Text, FileMode.Open); BinaryReader BR=NewBinaryReader (FS); Byte[] Bydata= Br. Readbytes ((int) fs. Length);//generating a binary streamFS.            Close (); //Convert a picture into a binary stream and store it in a database            stringconn ="server=.; Database=imagedb; Uid=sa; pwd=1234"; SqlConnection myconn=NewSqlConnection (conn); MyConn.            Open (); stringstr ="INSERT INTO ImageFile (Imagename,imagecontext) VALUES (@name, @file)"; SqlCommand Mycomm=NewSqlCommand (str, myconn); Mycomm. Parameters.Add ("@file", Sqldbtype.binary, bydata.length); Mycomm. parameters["@file"]. Value =Bydata; Mycomm. Parameters.Add ("@name", SqlDbType.NVarChar, -); Mycomm. parameters["@name"]. Value =ImageName; Mycomm. ExecuteNonQuery ();//writes a binary stream to the database, corresponding to the database image typemyconn.            Close (); //turn binary into imagestr ="Select top 1 imagecontext from ImageFile where Imagename= '"+ ImageName +"'"; MyConn=NewSqlConnection (conn); SqlDataAdapter SDA=NewSqlDataAdapter (STR, conn); DataSet myds=NewDataSet (); MyConn.            Open (); Sda.            Fill (myds); MyConn.            Close (); Byte[] Files= (byte[]) myds. tables[0]. rows[0]["Imagecontext"]; MemoryStream Ms=NewMemoryStream (Files); Image I=Image.fromstream (MS); pictureBox1.Image= i;//bind to the PictureBox1 control

C # pictures stored in SQL Server database

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.