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?
- 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 ();
- }
-
- }
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?
- 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;
- }
- }
-
- }
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