2

I have made a C# application that adds images from the harddisk to a blob-field in mysql. I don't get errors and the blob-field is filled.

When I check the blob-field in MySQL Workbench it says the field is not correct (it can not show the image).

Whatever I do, whatever example I try, nothing seems to work. So it has no point to place any example code here, because nothing is working so far.

Does anyone have a good example code that I can try?

Thanks

6
  • The code from your C# app might be useful; if for no reason but to verify that what you're doing is correct. Commented Sep 1, 2012 at 15:26
  • What kind of image is it? Maybe MySQL Workbench doesn't know how to show a TIF (for example). Try grabbing the bytes and seeing if you CAN view the view image. Commented Sep 1, 2012 at 15:29
  • @Cory: Some link is posted with code. I will check it, try it and posted here. Commented Sep 1, 2012 at 15:38
  • @aquinas: Nothing special, just a png 16x16. When I add the image directly with Workbench, it is showing correctly. Commented Sep 1, 2012 at 15:40
  • OK. Then POST YOUR CODE. Write a 5 line console application that can replicate the problem. Then we can help you. It's highly doubtful that MySQL has a problem. :) Commented Sep 1, 2012 at 15:49

3 Answers 3

3

Okay, since some people wanted some code to be shown, I created a very simple console application with the help described in the link of Alex. I literally taken that code and placed in the console app. Some miracle happend, because it worked as soon as I started the app. I don't know what I did wrong in the main application, but it has to be something (duh). Here is the code:

string MyConString = "SERVER=localhost;" +
    "DATABASE=database;" +
    "UID=root;" +
    "PASSWORD=pass;";

System.IO.FileStream fs = new FileStream(@"D:\link\to\image.png", FileMode.Open);
System.IO.BufferedStream bf = new BufferedStream(fs);
byte[] buffer = new byte[bf.Length];
bf.Read(buffer, 0, buffer.Length);    

byte[] buffer_new = buffer;

MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = new MySqlCommand("", connection);
command.CommandText = "insert into table(fldImage) values(@image);";

command.Parameters.AddWithValue("@image", buffer_new);

command.ExecuteNonQuery();

connection.Close();

Console.WriteLine("Task Performed!");
Console.ReadLine();
Sign up to request clarification or add additional context in comments.

1 Comment

Be careful in here. You never close the fs FileStream or the bf buffered stream. Your main problem could be that buffer_new doesn't contain all of the bytes of you image, resulting in a corrupted one in MySQL Workbench. You really should be applying the using statement to streams: using (var fs = new FileStream(@"D:\link\to\image.png", FileMode.Open)) { using (var bf = new BufferedStream(fs)) { /* your existing code */ } }
0

Did you try to recover the blob via the mysql connector (the way you store it). MySQL Workbench doesn't displaying the image doesn't mean that isn't being stored.

Try to recover the image:

while (reader.read())
{
    var image = (byte[])reader.getColumn(0);
    File.WriteAllBytes(@"c:\image.extension", image);
}

This way you can ensure it's saving.

2 Comments

Tried your code, image is saved, image can not be shown by windows image viewer. So... Now I can say the image is saved incorrect?
If you try with the proper extension then yes. It's not being saved the way it should. I'll suggest you to put the code you're using to see where's the problem.
0

I find when working with mysql and .NET that the MySQL Connector makes things really easy!

This guy: Save Image in MySQL via C# application seems to be on the right lines.

Good Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.