C # read MySQL blob field (reprint)

Source: Internet
Author: User
Tags connectionstrings

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

Development environment: Windows XP Professional SP3, VS2008, Winform, MySQL5.0, MySQL.Data.dll 6.2.3.0

1. Read a picture from your hard disk, convert it to a stream, and store it in this BLOB field

[CSharp]View Plaincopyprint?
  1. Private void button1_click (object sender, EventArgs e)
  2. {
  3. byte[] bytes = null;
  4. bytes = File.readallbytes (@"c:/documents and settings/user/my documents/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 (); //Can not be called here,
  19. Conn. Close (); //leave using block, connection shuts itself down
  20. }
  21. }

2. Read this BLOB field and convert it to a picture displayed on the PictureBox control

[CSharp]View Plaincopyprint?
  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. }

The database-related files are configured in app. Config, and if you don't use a configuration file, you can write:

String remote = "Persist Security info=false;database= database name; server= server ip;user id= user name; pwd= password";

Then Conn. ConnectionString = remote;

Postscript:

The MySQL library used in. NET was: Mysqldrivercs, but it was never done, and the Read BLOB field was also failed with the official feed. Instead of using MySql.Data.dll, note that the mysql.data5.0 version does not support reading BLOB fields, so a higher version is required, and I use MySQL.Data.dll 6.2.3.0.

MySql.Data.dll 6.2.3.0:http://download.csdn.net/source/2968152

MySql.Data.dll 5.0.9.0:http://download.csdn.net/source/2968157

C # read MySQL blob field (reprint)

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.