C # Read the MySQL blob Field

Source: Internet
Author: User
Tags connectionstrings

Development Environment: Windows XP Professional SP3, vs2008, winform, mysql5.0, MySQL. data6.2.3.0

 

1. Read an image from the hard disk, convert it to a stream, and store it in this blob field.

View plaincopy to clipboardprint?
  1. Private void button#click (Object sender, eventargs E)
  2. {
  3. Byte [] bytes = NULL;
  4. Bytes = file. readallbytes (@ "C:/Documents and Settings/user/My Documents ents/my pictures/11.jpg ");
  5. Using (MySQL. Data. mysqlclient. mysqlconnection conn = new mysql. Data. mysqlclient. mysqlconnection ())
  6. {
  7. Conn. connectionstring = configurationmanager. connectionstrings ["test"]. connectionstring;
  8. MySQL. Data. mysqlclient. mysqlcommand cmd = new mysql. Data. mysqlclient. mysqlcommand ();
  9. Cmd. commandtext = "insert into test (ID, picture) values (@ ID, @ picture )";
  10. Cmd. commandtype = commandtype. text;
  11. Cmd. Parameters. Add ("@ ID", MySQL. Data. mysqlclient. mysqldbtype. int32 );
  12. Cmd. Parameters. Add ("@ picture", MySQL. Data. mysqlclient. mysqldbtype. Blob );
  13. Cmd. Parameters [0]. value = 15;
  14. Cmd. Parameters [1]. value = bytes;
  15. Cmd. Connection = conn;
  16. Conn. open ();
  17. Int affectedrows = cmd. executenonquery ();
  18. Cmd. Dispose ();
  19. Conn. Close ();
  20. }
  21. }

Private void button#click (Object sender, eventargs e) <br/>{< br/> byte [] bytes = NULL; <br/> bytes = file. readallbytes (@ "C:/Documents and Settings/user/My Documents/my pictures/11.jpg"); <br/> using (MySQL. data. mysqlclient. mysqlconnection conn = new MySQL. data. mysqlclient. mysqlconnection () <br/>{< br/> Conn. connectionstring = configurationmanager. connectionstrings ["test"]. connectionstring; <br/> MySQL. data. mysqlclient. mysqlcommand cmd = new MySQL. data. mysqlclient. mysqlcommand (); <br/> cmd. commandtext = "insert into test (ID, picture) values (@ ID, @ picture)"; <br/> cmd. commandtype = commandtype. text; <br/> cmd. parameters. add ("@ ID", MySQL. data. mysqlclient. mysqldbtype. int32); <br/> cmd. parameters. add ("@ picture", MySQL. data. mysqlclient. mysqldbtype. BLOB); </P> <p> cmd. parameters [0]. value = 15; <br/> cmd. parameters [1]. value = bytes; <br/> cmd. connection = conn; <br/> Conn. open (); </P> <p> int affectedrows = cmd. executenonquery (); </P> <p> cmd. dispose (); <br/> Conn. close (); <br/>}</P> <p>}

 

2. Read this blob field and convert it to the image display on the picturebox control.

View plaincopy to clipboardprint?
  1. Private void button2_click (Object sender, eventargs E)
  2. {
  3. Using (MySQL. Data. mysqlclient. mysqlconnection conn = new mysql. Data. mysqlclient. mysqlconnection ())
  4. {
  5. Conn. connectionstring = configurationmanager. connectionstrings ["test"]. connectionstring;
  6. Conn. open ();
  7. MySQL. Data. mysqlclient. mysqlcommand cmd = new mysql. Data. mysqlclient. mysqlcommand ();
  8. Cmd. commandtype = commandtype. text;
  9. Cmd. commandtext = "select ID, picture from test where id = 11 ";
  10. Cmd. Connection = conn;
  11. System. Data. Common. dbdatareader reader = cmd. executereader ();
  12. Byte [] buffer = NULL;
  13. If (reader. hasrows)
  14. {
  15. Reader. Read ();
  16. Long Len = reader. getbytes (1, 0, null, 0, 0); // 1 is picture
  17. Buffer = new byte [Len];
  18. Len = reader. getbytes (1, 0, buffer, 0, (INT) Len );
  19. System. Io. memorystream MS = new system. Io. memorystream (buffer );
  20. System. Drawing. Image iamge = system. Drawing. image. fromstream (MS );
  21. Picturebox1.image = iamge;
  22. }
  23. }
  24. }

Private void button2_click (Object sender, eventargs e) <br/>{< br/> using (MySQL. data. mysqlclient. mysqlconnection conn = new MySQL. data. mysqlclient. mysqlconnection () <br/>{< br/> Conn. connectionstring = configurationmanager. connectionstrings ["test"]. connectionstring; <br/> Conn. open (); </P> <p> MySQL. data. mysqlclient. mysqlcommand cmd = new MySQL. data. mysqlclient. mysqlcommand (); <br/> cmd. commandtype = commandtype. text; <br/> cmd. commandtext = "select ID, picture from test where id = 11"; <br/> cmd. connection = conn; </P> <p> system. data. common. dbdatareader reader = cmd. executereader (); <br/> byte [] buffer = NULL; <br/> If (reader. hasrows) <br/>{< br/> reader. read (); <br/> long Len = reader. getbytes (1, 0, null, 0, 0); // 1 is picture <br/> buffer = new byte [Len]; <br/> Len = reader. getbytes (1, 0, buffer, 0, (INT) Len); </P> <p> system. io. memorystream MS = new system. io. memorystream (buffer); <br/> system. drawing. image iamge = system. drawing. image. fromstream (MS); <br/> picturebox1.image = iamge; <br/>}</P> <p>}

 

Database-related files are configured in APP. config. If you do not need a configuration file, you can write it as follows:

String remote = "Persist Security info = false; database = lhwtouch; server = Server IP address; user id = user name; Pwd = password ";

Then conn. connectionstring = remote.

 

 

Postscript:

 

The MySQL database used in. NET is mysqldrivercs, but it has never been done, and reading BLOB fields officially failed. Use MySQL. Data instead. Note that mysql. data5.0 does not support reading BLOB fields. Therefore, a higher version is required. I use MySQL. data6.2.3.0.

 

MySQL. data6.2.3.0: http://download.csdn.net/source/2968152

MySQL. data5.0.9.0: http://download.csdn.net/source/2968157

 

Code provider:Http://hi.csdn.net/DobzhanskyThank you!

 

From: http://blog.csdn.net/config_man/article/details/6123191

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.