Saving the Path vs the whole picture [duplicate] - c#

This question already has answers here:
Storing Images in DB - Yea or Nay?
(56 answers)
Closed 6 years ago.
I want to save the profile picture, and I want to know what is the best option.
-Saving the path in the DB and load the image according to his location.
-Save the whole image.
I'm using EntityFrameWork and SQL server Database

It depends on your application needs, Look at the answers in this post Storing Images in DB - Yea or Nay?
Storing images into database has some pros and cons
Images stored in the database do not require a different backup strategy. Images stored on filesystem do
It is easier to control access to the images if they are in a database. Idle admins can access any folder on disk. It takes a really determined admin to go snooping in a database to extract the images
On the other hand there are problems associated
Require additional code to extract and stream the images
Latency may be slower than direct file access
Heavier load on the database server

I suggest to save the image somewhere on file system and store path of that in database.
Advantages:
Images will be static files and can be served directly by Web Server and also can be cached easily.
DB server will not be overloaded with lot of request to fetch image data.
No need of dynamic code to serve files.
File system storage is less expensive then DB storage.
All over the web, most of the images are stored in filesystem.

Related

How to insert images into database using asp.net core mvc 6?

I'm trying to create a website. But i don't know how to upload photos into database. All information is outdated
If the size of your images are small, then you can convert it to base64, and store in your DB.
If the size is very larger, then you will face some issue.
The correct way to save pictures and Images
If the images are static resouce, and you can store it under wwwroot. Due to it will not often update, you can save it here.
If the images are create by web user, you also can follow the first suggestion, and create folder like wwwroot/userdata/userid/, and the path like wwwroot/userdata/userid/xx.jpg. The main disadvantage is that it is not conducive to maintenance.
The disadvantage is that the published files under wwwroot will become larger and larger, and the pressure on the web server will also increase, when many users access operations.
The best choice you can store images to storage account or other third party service.
In this way, our database only needs to save the path. Conducive to the maintenance of images data. There will be no stress on the web server during access and operation.

How to add images in db?

Currently working in an intern project, where i am required to add an image when adding an employee in my table.
we are using angularJS in front end and asp.net core 3.1 in backend, we have sql database using SSMS, i couldnt get it how to upload images, my senior told me to store the path in db, if i am to store the path in db, where will my images be uploaded, i did upload the images making an api on wwwroot folder, but they marked it as a bad practice? So can any of you guide me? Thankyou in advance :)
Steve's Comment is useful. You can upload image to the new folder under the wwwroot. You can refer the blog fisrt, it create a Resources folder, and it also use angular.
In general, we still do not recommend storing pictures under wwwroot. Regardless of whether you create a subfolder such as Sources.
Reason:
As the business increases, or over time, the contents under the folder will definitely increase. Even if the hard disk space at the deployment site is large, it will cause subsequent maintenance problems.
Specifically, the picture file may be accidentally deleted and cannot be recovered subsequently. If the picture file ends up taking up a lot of space, it will run out of hard disk space.
The Best Practice:
Upload the image to the cloud drive, something like: azure storage. And save the path to your database. It will save your disk space and it will also relieve your server stress, and more safety.
Additional tips:
If you just store a very small avatar file, we can convert it to base64 string format and store it in the database. In the absence of cloud services, some tiny images can be realized in this way.
However, this is not suitable for growing business scenarios, because it will cause the database footprint to become larger, which is not conducive to synchronization or backup. As well as poor large files, it may cause problems with the loading of files.

Best Way to insert images with MySQL?

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
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.
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.
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/

Save Image to HDD or store as Byte Array in database? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Storing Images in DB - Yea or Nay?
Pretty straight forward, I am hosting a site where users can upload pictures, and I have a .net File upload control working appropriately.. I'm just wondering what methodology I should use to store them on the server..
I can use the SaveAs() method off the FileUpload control which saves it as an actual file..
I can break the image down to a Byte[] and store it in the database for access.
I believe the real question here is.. Do I want the load on IIS or Sql Server 2008 R2?
What's the general consensus on which methodology I should use and why? Thanks!
There is no general consensus, because neither method is ideal.
Storing the images as files means that it becomes harder to scale the application to run on multiple servers. Then you would need a SAN/NAS to store the files instead of a local disk.
Storing the images in the database means that you are using a lot of the cache space for image data, slowing down other queries, and it also increases the size of the database, and the backups. It also means that you have to serve the images through a page, you can't just request the file directly.
So, it comes down to how much images you will have, and how scalable you need the application to be.
Avoid getting them in the database. That will make your DB a lot larger in size just because of a few files. That also affects the backup size.
I do not see any real gain on having the actual file bytes in the database. If you have the physical path to the file in the file system, that would suffice.
That also allows you to have your own backup strategy for the files.

Can anyone tell what is the best way to store images in SQL

I would like to store images in database by converting them in to binary objects or i will store them in to a temp folder and will save the path in my database. But i am unable to do the programming so can any give me a sample code to save images in to sql database using Asp.net.
Here we go with some links:
http://www.beansoftware.com/ASP.NET-Tutorials/Binary-Files-To-Database.aspx
http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
That should get you started, code wise.
better to store image in a folder and save the path in database, which would be much faster than saving in database.
Note dont use datatype image, which is going to change form database itself
: read this
Store the images path on DB instead of storing as Binary, because it hurts performance. In SQL server, BLOB datatypes are stored in seperate pages called LOB_data page and on data pages pointers will be linked with LOB_data page. You cannot create an index over BLOB data type also. Hence, it adversely affects the performance. Ideal solution would be to store the images in a shared drive and storing the image link in database.

Categories

Resources