C# 我应该使用什么变量类型来保存图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:13:15  来源:igfitidea点击:

What variable type should I use to save an image?

c#sqltypes

提问by

I'm going to be saving images into an SQL Database (don't know what type to use there), and I'm going to query the DB for an image of a students portrait shot.

我将把图像保存到 SQL 数据库中(不知道在那里使用什么类型),我将在数据库中查询学生肖像照的图像。

What variable type should I use 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 guidance please? :)

请问有什么指导吗?:)

采纳答案by marc_s

In your .NET code, you'll probably want to use System.Drawing.Imageor a derived class. Ultimately, you'll have to stream those bytes out to SQL Server one way or another, but you don't need to use a byte array from the beginning. All picture related types in .NET offer some kind of streaming support.

在您的 .NET 代码中,您可能想要使用System.Drawing.Image或 派生类。最终,您必须以某种方式将这些字节流式传输到 SQL Server,但您不需要从一开始就使用字节数组。.NET 中所有与图片相关的类型都提供某种流支持。

On the SQL Server side, by all means, use a VARBINARY(MAX)type - it's binary, it's up to 2 GB in size, it's fast, it's perfect for that use case. Up to an average image size of about 1 MB, this is probably your best bet - even better than using the SQL Server 2008 FILESTREAMattribute (which is great if you have lots of really really large images, larger than 1 MB on a regular basis; here, the binary file is itself stored in the server machine's file system under database control).

在 SQL Server 端,一定要使用一种VARBINARY(MAX)类型 - 它是二进制的,大小高达 2 GB,速度很快,非常适合该用例。平均图像大小不超过 1 MB,这可能是您最好的选择 - 甚至比使用 SQL Server 2008FILESTREAM属性更好(如果您有很多非常大的图像,定期大于 1 MB,这很好;在这里,二进制文件本身存储在数据库控制下的服务器机器的文件系统中)。

回答by womp

You have it correct - use a byte array in your class. For images less than 1 MB, use a varbinary(max) column in SQL server. If the images are over 1 MB and you're using SQL Server 2008, consider using a FILESTREAM column.

你说得对 - 在你的班级中使用字节数组。对于小于 1 MB 的图像,请在 SQL Server 中使用 varbinary(max) 列。如果图像超过 1 MB 并且您使用的是 SQL Server 2008,请考虑使用 FILESTREAM 列。

You can use the chart on this MSDN pageto refer to C#/Sql data type mappings.

您可以使用此 MSDN 页面上的图表来引用 C#/Sql 数据类型映射。

回答by Steve

Consider storing the image as a file and then reference the filename in the DB, not the binary data itself. This has several advantages, such as allowing you to store images on other servers and reducing the load/traffic on the SQL server.

考虑将图像存储为文件,然后在数据库中引用文件名,而不是二进制数据本身。这有几个优点,例如允许您将图像存储在其他服务器上并减少 SQL 服务器上的负载/流量。