In MySQL databases, there is a blob data type used to store files. C # to operate a MySQL database in a programming language, use the MySQL official component MySQL. Data. dll. Mysql. Data. dll (6.9.6) component: download. csdn. netdetailkeypig_zz in the MySQL database, there is a blob Data type used to store files. C # to operate a MySQL database in a programming language, use the MySQL official component MySQL. Data. dll.
Mysql. Data. dll (6.9.6) component: http://download.csdn.net/detail/keypig_zz/9262767.
Now let's talk about how to operate blob data.
Create a winform program and add two buttons. The code is as follows:
1 System. IO. memoryStream MS = new System. IO. memoryStream (); 2 private void button#click (object sender, EventArgs e) 3 {4 // Test serialization and deserialization 5 int [] arr = {1, 2, 3 }; 6 BinaryFormatter bFormatter = new BinaryFormatter (); 7 bFormatter. serialize (ms, arr); 8 byte [] byteArr = ms. toArray (); 9 MessageBox. show (byteArr. length. toString (); 10 MySqlConnection conn = new MySqlConnection (Properties. settings. default. MySqlConnectString); 11 // string insertStr = "update mm set aa = 4, arr = @ blobData where aa = 4;"; 12 string insertStr = "insert into mm (arr) values (@ blobData); "; // you need to set the auto-incrementing 13 MySqlParameter par = new MySqlParameter (" @ blobData ", MySqlDbType. blob); 14 par. value = byteArr; 15 MySqlCommand cmd = new MySqlCommand (insertStr, conn); 16 cmd. parameters. add (par); 17 try18 {19 conn. open (); 20 cmd. executeNonQuery (); 21 ms. clos E (); 22 ms. dispose (); 23} 24 catch (Exception ep) 25 {26 MessageBox. show (ep. message); 27} 28 29 30} 31 32 private void button2_Click (object sender, EventArgs e) 33 {34 BinaryFormatter bFormatter = new BinaryFormatter (); 35 36 MySql. data. mySqlClient. mySqlDataReader myData; 37 MySqlConnection conn = new MySql. data. mySqlClient. mySqlConnection (Properties. settings. default. mySqlConnectString); 38 string readStr = "Select arr from mm where id = 6;"; 39 MySqlCommand cmd = new MySqlCommand (readStr, conn); 40 try41 {42 conn. open (); 43 myData = cmd. executeReader (); 44 if (! MyData. hasRows) 45 {46 throw new Exception ("No blob data"); 47} 48 myData. read (); 49 byte [] blob = new byte [myData. getBytes (0, 0, null, 0, int. maxValue)]; 50 myData. getBytes (0, 0, blob, 0, blob. length); 51 myData. close (); 52 MS = new System. IO. memoryStream (blob); 53 ms. position = 0; 54 int [] arr = (int []) bFormatter. deserialize (ms); 55 ms. dispose (); 56 string arrStr = null; 57 for (int I = 0; I <arr. getLength (0); I ++) 58 {59 arrStr + = arr [I]. toString () + ""; 60} 61 MessageBox. show (arrStr); 62} 63 catch (Exception ep) 64 {65 MessageBox. show (ep. message); 66} 67 68}
The connection string in the code is stored in Settings. the specific format is: "server = 127.0.0.1; user = root; database = ***; port = 3306; password = ***;". A table named mm exists in the database and contains an id field (primary key, auto-increment) and an arr field (blob type ).
The code above serializes an integer array {, 3} and stores it in the MySQL database as blob. Further, it reads blob data from the MySQL database for deserialization, obtain the integer array {1, 2, 3 }. The code execution result is as follows: