MySQL Connector/NET Developer Guide / ... / Connector/NET Programming / Handling BLOB Data With Connector/NET / Reading a BLOB from the Database to a File on Disk
After a file is loaded into thefile table, we can use theMySqlDataReader class to retrieve it.
The following code retrieves a row from thefile table, then loads the data into aFileStream object to be written to disk:
C# Code Example
MySql.Data.MySqlClient.MySqlConnection conn;MySql.Data.MySqlClient.MySqlCommand cmd;MySql.Data.MySqlClient.MySqlDataReader myData;conn = new MySql.Data.MySqlClient.MySqlConnection();cmd = new MySql.Data.MySqlClient.MySqlCommand();string SQL;UInt32 FileSize;byte[] rawData;FileStream fs;conn.ConnectionString = "server=127.0.0.1;uid=root;" + "pwd=12345;database=test";SQL = "SELECT file_name, file_size, file FROM file";try{ conn.Open(); cmd.Connection = conn; cmd.CommandText = SQL; myData = cmd.ExecuteReader(); if (! myData.HasRows) throw new Exception("There are no BLOBs to save"); myData.Read(); FileSize = myData.GetUInt32(myData.GetOrdinal("file_size")); rawData = new byte[FileSize]; myData.GetBytes(myData.GetOrdinal("file"), 0, rawData, 0, (int)FileSize); fs = new FileStream(@"C:\newfile.png", FileMode.OpenOrCreate, FileAccess.Write); fs.Write(rawData, 0, (int)FileSize); fs.Close(); MessageBox.Show("File successfully written to disk!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); myData.Close(); conn.Close();}catch (MySql.Data.MySqlClient.MySqlException ex){ MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);}Visual Basic Code Example
Dim conn As New MySqlConnectionDim cmd As New MySqlCommandDim myData As MySqlDataReaderDim SQL As StringDim rawData() As ByteDim FileSize As UInt32Dim fs As FileStreamconn.ConnectionString = "server=127.0.0.1;" _ & "uid=root;" _ & "pwd=12345;" _ & "database=test"SQL = "SELECT file_name, file_size, file FROM file"Try conn.Open() cmd.Connection = conn cmd.CommandText = SQL myData = cmd.ExecuteReader If Not myData.HasRows Then Throw New Exception("There are no BLOBs to save") myData.Read() FileSize = myData.GetUInt32(myData.GetOrdinal("file_size")) rawData = New Byte(FileSize) {} myData.GetBytes(myData.GetOrdinal("file"), 0, rawData, 0, FileSize) fs = New FileStream("C:\newfile.png", FileMode.OpenOrCreate, FileAccess.Write) fs.Write(rawData, 0, FileSize) fs.Close() MessageBox.Show("File successfully written to disk!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) myData.Close() conn.Close()Catch ex As Exception MessageBox.Show("There was an error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)End Try After connecting, the contents of thefile table are loaded into aMySqlDataReader object. TheGetBytes method of theMySqlDataReader is used to load theBLOB into a byte array, which is then written to disk using aFileStream object.
TheGetOrdinal method of the MySqlDataReader can be used to determine the integer index of a named column. Use of the GetOrdinal method prevents errors if the column order of theSELECT query is changed.