C # Read the Mysqlblob Field

Source: Internet
Author: User
Development Environment: WindowsXPProfessionalSP3, VS2008, Winform, MySQL5.0, MySQL. Data6.2.3.01. Read an image from the hard disk, convert it to a stream, and store it in this BLOB field viewplaincopytoclipboardprint? Privatevoidbutton#click (objectsender, EventArgse) {byte

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 the BLOB field view plaincopy to clipboardprint? Private void button#click (object sender, EventArgse) {byte

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) {byte [] bytes = null; bytes = File. readAllBytes (@ "C:/Documents and Settings/user/My Documents ents/My Pictures/11.jpg"); using (MySql. data. mySqlClient. mySqlConnection conn = new MySql. data. mySqlClient. mySqlConnection () {conn. connectionString = ConfigurationManager. connectionStrings ["test"]. connectionString; MySql. data. mySqlClient. mySqlCommand cmd = new MySql. data. mySqlClient. mySqlCommand (); cmd. commandText = "insert into test (id, picture) values (@ id, @ picture)"; cmd. commandType = CommandType. text; cmd. parameters. add ("@ id", MySql. data. mySqlClient. mySqlDbType. int32); cmd. parameters. add ("@ picture", MySql. data. mySqlClient. mySqlDbType. blob); cmd. parameters [0]. value = 15; cmd. parameters [1]. value = bytes; cmd. connection = conn; conn. open (); int affectedrows = cmd. executeNonQuery (); cmd. dispose (); conn. close ();}}

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) {using (MySql. data. mySqlClient. mySqlConnection conn = new MySql. data. mySqlClient. mySqlConnection () {conn. connectionString = ConfigurationManager. connectionStrings ["test"]. connectionString; conn. open (); MySql. data. mySqlClient. mySqlCommand cmd = new MySql. data. mySqlClient. mySqlCommand (); cmd. commandType = CommandType. text; cmd. commandText = "select id, picture from test where id = 11"; cmd. connection = conn; System. data. common. dbDataReader reader = cmd. executeReader (); byte [] buffer = null; if (reader. hasRows) {reader. read (); long len = reader. getBytes (1, 0, null, 0, 0); // 1 is picture buffer = new byte [len]; len = reader. getBytes (1, 0, buffer, 0, (int) len); System. IO. memoryStream MS = new System. IO. memoryStream (buffer); System. drawing. image iamge = System. drawing. image. fromStream (MS); pictureBox1.Image = iamge ;}}}

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!

Http://blog.csdn.net/config_man/article/details/6123191

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.