C # SQL Server database image access

Source: Internet
Author: User

C # SQL Server database image access

# Region database image access //////Import images to the database//////Public void Import (string filePath) {string fileName = filePath. substring (filePath. lastIndexOf (@) + 1, filePath. lastIndexOf (.) -filePath. lastIndexOf (@)-1); FileStream fileStream = new FileStream (filePath, FileMode. open); byte [] imageBytes = new byte [fileStream. length]; BinaryReader binaryReader = new BinaryReader (fileStream); imageBytes = binaryReader. readBytes (Convert. toInt32 (fileStream. length); SqlConnection sqlConnection = new SqlConnection (@ data source = PANLEE-PCMSSQLSERVER_2; initial catalog = DBImage; integrated security = true); sqlConnection. open (); SqlCommand sqlCommand = new SqlCommand (); sqlCommand. connection = sqlConnection; try {string sqlCreate = @ Create Table Portraits (student ID varchar (50), photo image,); sqlCommand. commandText = sqlCreate; sqlCommand. executeNonQuery ();} catch {} sqlCommand. commandText = insert into Portraits (student ID, photo) values (@ ID, @ Image); sqlCommand. parameters. add (Image, SqlDbType. image); sqlCommand. parameters. add (ID, SqlDbType. varChar); sqlCommand. parameters [ID]. value = fileName; sqlCommand. parameters [Image]. value = imageBytes; sqlCommand. executeNonQuery (); sqlConnection. close ();}//////Export Images/////////
 
  
Bitmap
 Public Bitmap Export (string SID) {byte [] imagebytes = null; SqlConnection sqlConnection = new SqlConnection (@ data source = PANLEE-PCMSSQLSERVER_2; initial catalog = DBImage; integrated security = true); sqlConnection. open (); SqlCommand sqlCommand = new SqlCommand (select photo from Portraits where student ID = @ ID, sqlConnection); sqlCommand. parameters. add (ID, SqlDbType. varChar); sqlCommand. parameters [ID]. value = SID; SqlDataReader sqlDataReader = sqlCommand. executeReader (); while (sqlDataReader. read () {imagebytes = (byte []) sqlDataReader. getValue (0);} sqlDataReader. close (); sqlCommand. clone (); sqlConnection. close (); MemoryStream MS = new MemoryStream (imagebytes); Bitmap bitmap = new Bitmap (MS); return bitmap;} # endregion

 

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.