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?
- Private void button1_click (object sender, EventArgs e)
- {
- byte[] bytes = null;
- bytes = File.readallbytes (@"c:/documents and settings/user/my documents/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 (); //Can not be called here,
- Conn. Close (); //leave using block, connection shuts itself down
- }
- }
2. Read this BLOB field and convert it to a picture displayed on the PictureBox control
[CSharp]View Plaincopyprint?
- 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;
- }
- }
- }
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)