SQLite stores binary data

Source: Internet
Author: User

In C #:

 Class Program  {  Static void main  (  String  []  ARGs  )  {  If  (  File  .  Exists  (  "Test. db3"  ))  {  File  .  Delete  ( "Test. db3"  );  }  Using  (  VaR connection  =  New sqliteconnection  (  "Data Source = test. db3; version = 3"  ))  Using  (  VaR command  =  New sqlitecommand  (  "Create table photos (ID integer primary key autoincrement, photo BLOB )"  ,  Connection ))  {  Connection  .  Open  ();  Command  .  Executenonquery  ();  Byte  []  Photo  =  New byte  []  {  1  ,  2  ,  3 ,  4  ,  5  };  Command  .  Commandtext  =  "Insert into Photos (photo) values (@ photo )"  ;  Command  .  Parameters  .  Add  (  "@ Photo"  ,  Dbtype  . Binary  ,  20  ).  Value  =  Photo  ;  Command  .  Executenonquery  ();  Command  .  Commandtext  =  "Select photo from photos where id = 1"  ;  Using  (  VaR Reader =  Command  .  Executereader  ())  {  While  (  Reader  .  Read  ())  {  Byte  []  Buffer  =  Getbytes  (  Reader  );  } }  }  }  Static byte  []  Getbytes  (  Sqlitedatareader Reader  )  {  Const int chunk_size  =  2  *  1024  ;  Byte  []  Buffer  =  New byte  [ Chunk_size  ];  Long bytesread  ;  Long fieldoffset  =  0  ;  Using  (  Memorystream stream  =  New memorystream  ())  {  While  ((  Bytesread  =  Reader  . Getbytes  (  0  ,  Fieldoffset  ,  Buffer  ,  0  ,  Buffer  .  Length  ))  >  0  )  {  Stream  .  Write  (  Buffer ,  0  ,  (  Int  )  Bytesread  );  Fieldoffset  + =  Bytesread  ;  }  Return  Stream  .  Toarray  ();  }  }  } 
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.