What type of variable should be used to save the image?

I am going to save images to an SQL database (I don’t know which type to use there), and I am going to query the DB for the portrait portrait image of students.

What type of variable should be used to store this image?

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Nana_s_Student_Gradebook { class Student { public string name { get; set; } public string lastName { get; set; } public byte[] picture { get; set; } public DateTime dateOfBirth { get; set; } } } 

Any directions please? :)

+7
c # types sql
source share
4 answers

On the SQL Server side, by all means, use the VARBINARY(MAX) - this is a binary file, up to 2 GB in size, fast, it is ideal for this use case. Up to an average image size of about 1 MB, this is probably the best choice - even better than using the SQL FILESTREAM SQL Server 2008 attribute (which is great if you have many really large images larger than 1 MB per regular, here the binary file itself is stored in the file a server system of a machine running a database).

In your .NET code, you probably want to use Image or derived classes β€” you end up having to pass these bytes to SQL Server anyway, but you don’t need to use an array of bytes from the beginning β€” all the mapping types in .NET offer some streaming support.

+7
source share
0
source share

This is correct for you - use an array of bytes in your class. For images smaller than 1 MB, use the varbinary (max) column on the SQL server. If the images are larger than 1 MB and you are using SQL Server 2008, consider using the FILESTREAM column.

You can use the diagram on this MSDN page to refer to C # / Sql data type associations.

0
source share

Consider saving the image as a file, and then a link to the file name in the database, and not to binary data. This has several advantages, for example, it allows you to store images on other servers and reduce the load / traffic on the SQL server.

0
source share

All Articles