Why use Bytes instead of Image? What is the best practice?

Asked

Viewed 4,039 times

6

I would like to know why to use the data type Bytes instead of using one’s own Image. If the SQL Server database has the Image data type, then it should be easier and more practical to enter in mode Image right?

I would like an example of how to take the form image and save it to an SQL Server database, and then retrieve it to a View.

  • I see there 2 questions, a good and an improper.

2 answers

8


I see some reasons for this. The first of them is that the type of data Image will be removed in some future version of SQL Server.(MSDN) Data types ntext, text and image will be removed in a future version of Microsoft SQL Server

The Image library System.Drawing is not equivalent to the field Image Sql Server, and they didn’t do any "mapping" for it. I agree that it would even be simpler, however, it is not difficult to convert an image into an array of bytes:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    return  ms.ToArray();
}

System.Drawing.Imaging.ImageFormat.Gif you substitute for whatever format your image is. (MSDN) Image Formats. I removed this code from Stackoverflow.com (question 17352061)

All the more, this field image contains nothing more than Dados binários do comprimento variável de 0 a 2^31-1 (2.147.483.647) bytes., how you cite the first msdn link I sent.

I hope I helped. As for example, take a search on google, it is very easy to find.

  • Another reason is that we can upload and save files of any kind that are not necessarily images.

  • Great answer! Would you explain to me how to limit to receive files of such extension? Because on my page I want to receive only . jpeg (.jpg is the same thing I believe) and . png

  • @Luiznegrini receive files that have the extension . jpg, or receive only image files? Because it is possible to rename a file. pdf (e.g. to . jpg, and your website would accept it as an image.

  • @woliveirajr would receive only image and only with these extensions.

  • @Luiznegrini you could ask a new question about this, would be cool. How to restrict upload to only certain extensions, and how to determine that content is a valid jpeg file

  • @woliveirajr ok, thanks a lot!

  • Nothing you determine would be by comic book. File extension you do by the client, if it is a valid jpeg only after you complete the upload you will be able to process the file in some way and again is something that the application could do.

Show 2 more comments

0

Another solution is not to store blobs in the database in any way. This is especially good for performance when you upload/donwload large files. From experience I have seen that it is much more efficient to store the files in a repository and only save a link/path to it in the database.

  • SQL Server has a data type for this.

  • All DBMS have, so that they don’t get "fewer features than your competitors" but having the functionality does not mean that you need to use it or that it provides the best solution. Certainly 100% of features do not offer best practice in 100% of scenarios. This is one of them.

  • I believe that nowadays the main ones do it. SQL Server did not always do it. I have never used and I may be wrong, but One of the things that SQL Server does is to integrate these files with the rest of BD. If I’m not mistaken, the latest versions even give "direct" access to the file. I see only advantages. Or I’m wrong?

  • The 2012 version brought the Filetables that could be used in this case but I have not yet tested the performance. Anyway what would be the advantage of keeping these images at the base in a hypothetical case of you having 1TB of data and 2TB of images?

  • Referential integrity. Management of backups. Consistency of backups.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.