-2

I will create MySQL database for online store. This store has around 3000 products. Each product has 5 photos. I created tables for products table (product_id, product_name) and images table (image_id, Image_details, Product_id)

What is best way to insert photo? *i am using C# (desktop app) as GUI

David Walschots
  • 12,279
  • 5
  • 36
  • 59
Thamer1980
  • 25
  • 1
  • 5
    The best way is to not do it, relational databases don't deal with large binary attachments very well, they complicate backups, and are just inefficient to start with due to how many layers the system must navigate to fetch and return image data. Use a filesystem or an object-store with a reference to the image stored in the database. – tadman Sep 19 '18 at 17:23
  • Thank You. Could you please write more details – Thamer1980 Sep 19 '18 at 17:47
  • It depends on what you're using server-side. Client-side is mostly irrelevant. – tadman Sep 19 '18 at 18:24

3 Answers3

2

Although, with BLOB objects you can achieve this, it not the preferred way. You have to store the location of the images (similarly for other large files), when the query result turned, use the location to access the images.

Keep in mind, your images will be increased over time, so, in practice, we apply a directory hierarchy, usually two will be enough. One can use the first two letter of the name of the image, however, this will be unbalanced directory structure. The better idea is using hash based randomization to distribute the files evenly over the sub-directories. An example (in Java) can be found here.

kelalaka
  • 5,064
  • 5
  • 27
  • 44
1

Dont save your images in the database, it's not the best way. save the paths to the images in your table related to the products.

Amazigh.Ca
  • 2,753
  • 2
  • 11
  • 8
  • i will save URL inside Images Table. But what about Image? i will put my database on Hosting Company. – Thamer1980 Sep 19 '18 at 17:47
  • You can save them in your hosting server with uploading them. Or use a third party cloud services like amazon, or even like Google photos... – Amazigh.Ca Sep 19 '18 at 17:58
1

Use cloud Blob storages or FileSystem instead of saving in your relational database. It is not recommended storing a lot of images >1mb in your database due performance, backup and maintenability.

However if you have a small app and are really sure about going this way, you could convert your image to a byte array and save in a MySQL BLOB field. Check the link below for a sample:

C# saving images to MySql database as blob

Check:

Should I store my images in the database or folders?

How can I save an Image to the File System?

https://learn.microsoft.com/pt-br/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=windows

https://www.c-sharpcorner.com/article/azure-storage-crud-operations-in-mvc-using-c-sharp-part-two/

Felipe Torres
  • 292
  • 2
  • 10