Binary data will not be properly passed to your insertion since you are using string concatenation - you will get rawData.ToString() , which will probably just print the type name (hence your binary data is 13 bytes long compared to file size> 3000 bytes ); try this instead:
byte[] rawData = File.ReadAllBytes(@"d:\Untitled.gif"); FileInfo info = new FileInfo(@"d:\Untitled.gif"); int fileSize = Convert.ToInt32(info.Length); using(MySqlConnection connection = new MySqlConnection("server=192.168.1.104;uid=root;pwd=root;database=cady234;")) { using(MySqlCommand command = new MySqlCommand()) { command.Connection = connection; command.CommandText = "INSERT INTO file (file_name, file_size, file) VALUES (?fileName, ?fileSize, ?rawData);"; MySqlParameter fileNameParameter = new MySqlParameter("?fileName", MySqlDbType.VarChar, 256); MySqlParameter fileSizeParameter = new MySqlParameter("?fileSize", MySqlDbType.Int32, 11); MySqlParameter fileContentParameter = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length); fileNameParameter.Value = "test name"; fileSizeParameter.Value = fileSize; fileContentParameter.Value = rawData; command.Parameters.Add(fileNameParameter); command.Parameters.Add(fileSizeParameter); command.Parameters.Add(fileContentParameter); connection.Open(); command.ExecuteNonQuery(); } }
I have presented several concepts for you here; firstly, if you are going to immediately download all binary data, just use the static File.ReadAllBytes method - this is much less code.
Secondly, there is no need to use the full namespace every time - use the using directive
Thirdly, (a little vaguely) there is also a using statement in C #. This ensures that any object that implements IDisposable is correctly cleaned after itself. In the case of a connection, it will explicitly call Close and Dispose if the command succeeds or fails.
Finally, I customized your request. Parameters are useful for many reasons; they help protect against SQL Injection , in which case they must also ensure that your data types are being processed correctly. You can learn more about SqlParameter (which, for example, MySqlParameter is a database implementation but uses the same principles).
Tested with MySQL 5.5.15, MySQL Connector 5.2.7 runs under .Net 4