0

I am making a program which requires getting the local directory of an image from a MySQL database table. For the moment, I have made the directory of the image I want to retrieve equal to C:\Users\User\Pictures\PictureName.PNG in the MySQL table. The method I have written up is able to retrieve this data from the database through a Select statement and set the PictureBox image as a new Bitmap with the path retrieved from the Selectstatement.

I couldn't find the information I was looking for online as most of the answers relate to using the BLOB field in MySQL to store images, but my question is if I were to put my database on a server and with the pictures on there too, would my method scale to accommodate for those changes or would I have to add other functionality in order for it to work on a server as well?

Here is my code below:

public void setImage() {
    string path = sql.RetrieveImage(SelectQuery.RetrievePicture());
    PictureBox1.Image = new Bitmap(path);
}

public string RetrieveImage(Query query) {
    string path = "";

    // OpenDatabaseConnection() will open up a connection to the database
    // through connection and if successful, will return true.
    if (this.OpenDatabaseConnection()) {

        // query.ToSql() returns a string format of the select statement
        // required to retrieve the local image directory 

        using (MySqlCommand cmd = new MySqlCommand(query.ToSql(), connection)) {
            MySqlDataReader dataReader = cmd.ExecuteReader();
            dataReader.Read();
            path = dataReader[0] + "";
            dataReader.Close();
        }

        this.CloseDatabaseConnection();
    }
    return path;
}
Hayden
  • 407
  • 1
  • 6
  • 15

1 Answers1

0

Storing images in a DB is a bad idea. Storing paths to image files is a much better way to go, IMO.

This SO question has a few links to really good supporting information.

If you must go that route, however, then yes, the BLOB field (or a VarBinary(MAX) in SQLServer) is the way to do it. You can retrieve those images as Byte[] data.

Community
  • 1
  • 1
DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • I didn't say that I was actually storing the actual image as a BLOB, just the directory. I will look at the link though. Thank you. – Hayden Apr 22 '14 at 11:41
  • Thank you for referring me to the link, it did give me an idea on how to achieve my goal. – Hayden Apr 22 '14 at 11:46
  • @Don Why is it a bad idea though to store images in the database? I am going to store the software logo in the database so in case it gets changed all the client applications will get the new one from the database – John Demetriou Oct 10 '14 at 10:35
  • @JohnDemetriou Generally speaking, file access to images is faster and more reliable. Using a DB, there is always the need to pull the data and then form an Image construct from it. These mechanics are automatic in most cases via file I/O. Metadata can be an issue. DB size compounds quickly with image data. The list goes on... Follow the linked issue in my answer, it leads to many discussions on SO alone on this topic. – DonBoitnott Oct 10 '14 at 11:11
  • yeah but given is only one image for a logo and not a whole DB of images the overhead is really small I suppose – John Demetriou Oct 10 '14 at 11:25