Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone, I did ask this question previously, but in that moment I was trying to save the route of an image to the database, copy that image, and then show it in my view, but now, i'm just searching the way to upload an image to the database, using SQL Server and ASP.NET MVC 5, sorry for repeat almost the same question, but they are different, please, somebody help me, I have the same problem for almost a week.
Posted
Comments
[no name] 25-Sep-14 13:32pm    
And there must be thousands of examples for you to look at if you go look for them.
Kschuler 25-Sep-14 14:07pm    
search for the keyword "BLOB" which stands for Binary Large Object something or other.

Background

You need to just find an answer for the ASP.NET, MVC is just a sub category of the ASP.NET web applications or web sites. ASP.NET is built directly oowrhe stuff that works with .NET Framework would work in ASP.NET too.

Finding the code

This project (problem) has been up for many developers, for using the SQL Server and storing images in it. There are many projects or articles for it. The basic implementation to it is,


  • A simple ASP.NET code to handle the web requests.
  • SQL Server database connection
  • Web page to display it


Handling web request

You can easily handle the web requests of uploading the files, a simple request for that is like,

C#
if(IsPost) {
   // make sure form has method set to post.
   // get the image
   var image = WebImage.GetImageFromRequest();
}


SQL Server setup

SQL Server, mostly CE is used but Microsoft is now deprecating this version, so it is better for you to use the SQL Server Express, or any other version if required, all you need to make sure is that the connection is established and that the columns are good to store the data. By "good to store" I mean that the data type for each of the column must be set, in SQL Server, images file, (or generally files) are stored in the form of bytes, so the Image data type in the SQL Server is for saving the files, as called the BLOBS or bytes etc. So to store these files in the database you need to have a column of type Image.

Also, there is a limit on this Image data type of the SQL too, as stated in the document link:

Quote:
Variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes.


http://msdn.microsoft.com/en-us/library/ms187993.aspx[^]

Saving and getting files from SQL Server Database

This is the task where you need to be told about how to and where to write the code. ASP.NET MVP Mike Brind has written an excellent article for this, http://www.mikesdotnetting.com/Article/148/Save-And-Retrieve-Files-From-a-Sql-Server-CE-Database-with-WebMatrix[^]

You can read the above article to learn specifically about ASP.NET and the SQL Server CE. There is no general difference between working of SQL Server and SQL Server CE, just a bit of feature lackages like stored procedures aren't present in the SQL Server CE. Below is another article posted at CodeProject relating to this one, but I myself prefer the Mike's article.

C# Save and Load Image from Database[^]

Personal opinion

Through my personal experience of developmental progress, I have learnt that saving the files inside the Database isn't a good way of storing the files. You should always save the files in the File System, and just save their links in the database.
 
Share this answer
 
v2
 
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