Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all. In my project i need to save user uploaded images into sql Server database.I thought to store those images directly into database by taking Image type column and i will retrieve them whenever i need to.But some of my friends are saying that this will result into performance issues as the app need to download those images directly from database.So,they are suggesting to save only the paths of images in database.but here my problem is what will be the situation if any image is not available in the stored path or missing by any chance?.I am totally confused in this scenario.So, guys please guide me according to this scenario where i can implement secured approach and i can avoid performance at the same time

Thanks in Advance
Posted

Here you need to decide based on requirement.
Whether the total size of expected images would easlily be stored on database. Storing image in DB would obiously will increase load on DB.

Storing the path of image in database is better in case your database server degrades if saved in DB. Also storing image in file system will provide much more flexibility and scalability.

Regarding securty if save images in file system, I think it will be stored on some secured server and these will not be accessible to all. So if your server is secured enough then I don't see any security issue.

So decide based on your size of the images and database server capability.
 
Share this answer
 
// CREATE DATABASE CONNECTION
        using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString))
        {
            // OPEN CONNECTION
            con.Open();

            // READ IMAGE BYTES
            byte[] bt = System.IO.File.ReadAllBytes(@"D:\image.jpg");

            // CREATE SQLCOMMAND AND SPECIFY PROPERTIES
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();

            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "your_storedprocedure_name";

            // ASSIGN IMAGE TO StoredProcedure PARAMETER
            System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter("@imgdata", bt);
            cmd.Parameters.Add(param);

            // EXECUTE StoredProcedure TO SAVE IMAGE
            cmd.ExecuteNonQuery();

            // CLOSE CONNECTION
            con.Close();
        }
 
Share this answer
 
What your friends suggesting is right. saving images in database will decrease in performance and hence you will save path or imagename only in database.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900