如何在数据库中保存图片?

时间:2022-12-29 15:50:45

i'm working on Linq To Sql,WPF and i have a database now i need to save some picture in the database but i don't know which is the correct datatype to save the pictures Database(this database would be connect from 10 users in the same time). Can you point me in the right way to overcome this step? If i didn't wrong it is not a good idea to save pictures in the database but if you can advice me a better method i will apply it. Thanks so much for your time.

我正在研究Linq To Sql,WPF和我有一个数据库现在我需要在数据库中保存一些图片,但我不知道哪个是正确的数据类型来保存图片数据库(这个数据库将连接10个用户在同一时间)。你能指出我正确的方法来克服这一步吗?如果我没有错,那么在数据库中保存图片并不是一个好主意,但如果你能给我一个更好的方法,我会应用它。非常感谢你的时间。

Nice Regards

5 个解决方案

#1


You can use a 'varbinary(MAX)' or 'image' column type. Linq2Sql will auto-generate a class that uses a Binary object to wrap your image. The Binary object is just a wrapper around a byte[].

您可以使用'varbinary(MAX)'或'image'列类型。 Linq2Sql将自动生成一个使用二进制对象来包装图像的类。 Binary对象只是一个byte []的包装器。

myObject.Image = new Binary(imageByteArray);

#2


Store your picture as a blob, the variable defined in your class containing the image could be a byte[] stream. Alternatively you just store a reference to the picture in the database and store the image on a file server.

将图片存储为blob,包含图像的类中定义的变量可以是byte []流。或者,您只需在数据库中存储对图片的引用,并将图像存储在文件服务器上。

#3


Typically you will use a varbinary(max) -or less than max- on the database side and you will use a byte[] type in your class.

通常,您将在数据库端使用varbinary(max) - 或小于max-,并且您将在类中使用byte []类型。

#4


There's a lot of heated debates that occur when people talk about this, I would like to note that you might want to consider storing that path to a network folder in the database.

当人们谈论这个问题时会发生很多热烈的争论,我想请注意,您可能要考虑将该路径存储到数据库中的网络文件夹中。

The disadvantage of storing the actual image in the database is that all those bytes have to get sent back and forth through a sql query and if those images are large you will be increasing the size of your db substantially. along with the weird things that were mentioned above.

将实际图像存储在数据库中的缺点是所有这些字节必须通过sql查询来回发送,如果这些图像很大,你将大大增加数据库的大小。以及上面提到的奇怪的事情。

Anyways I don't want to open up a can of worms, just wanted to show an alternative.

无论如何我不想打开一罐蠕虫,只是想展示另一种选择。

UPDATE:

Something like this:

像这样的东西:

public partial class LinqClass
{
    public string ImagePath { get; set; }

    public System.Drawing.Image Picture
    {
        get
        {
            return System.Drawing.Image.FromFile(ImagePath);
        }
    }
}

where ImagePath is the actual column in the db that you are saving the file path to. This doesn't have the code to save the file (something like File.Save(ImagePath) etc. but it's a start.

其中ImagePath是您要保存文件路径的数据库中的实际列。这没有保存文件的代码(类似File.Save(ImagePath)等),但它是一个开始。

#5


I've never done it with Linq, but we used a b64 conversion for the image, then a clob datatype. Then reverse the b64 when you want to view the image.

我从来没有用Linq做过,但我们使用b64转换为图像,然后使用clob数据类型。当您想要查看图像时,请反转b64。

#1


You can use a 'varbinary(MAX)' or 'image' column type. Linq2Sql will auto-generate a class that uses a Binary object to wrap your image. The Binary object is just a wrapper around a byte[].

您可以使用'varbinary(MAX)'或'image'列类型。 Linq2Sql将自动生成一个使用二进制对象来包装图像的类。 Binary对象只是一个byte []的包装器。

myObject.Image = new Binary(imageByteArray);

#2


Store your picture as a blob, the variable defined in your class containing the image could be a byte[] stream. Alternatively you just store a reference to the picture in the database and store the image on a file server.

将图片存储为blob,包含图像的类中定义的变量可以是byte []流。或者,您只需在数据库中存储对图片的引用,并将图像存储在文件服务器上。

#3


Typically you will use a varbinary(max) -or less than max- on the database side and you will use a byte[] type in your class.

通常,您将在数据库端使用varbinary(max) - 或小于max-,并且您将在类中使用byte []类型。

#4


There's a lot of heated debates that occur when people talk about this, I would like to note that you might want to consider storing that path to a network folder in the database.

当人们谈论这个问题时会发生很多热烈的争论,我想请注意,您可能要考虑将该路径存储到数据库中的网络文件夹中。

The disadvantage of storing the actual image in the database is that all those bytes have to get sent back and forth through a sql query and if those images are large you will be increasing the size of your db substantially. along with the weird things that were mentioned above.

将实际图像存储在数据库中的缺点是所有这些字节必须通过sql查询来回发送,如果这些图像很大,你将大大增加数据库的大小。以及上面提到的奇怪的事情。

Anyways I don't want to open up a can of worms, just wanted to show an alternative.

无论如何我不想打开一罐蠕虫,只是想展示另一种选择。

UPDATE:

Something like this:

像这样的东西:

public partial class LinqClass
{
    public string ImagePath { get; set; }

    public System.Drawing.Image Picture
    {
        get
        {
            return System.Drawing.Image.FromFile(ImagePath);
        }
    }
}

where ImagePath is the actual column in the db that you are saving the file path to. This doesn't have the code to save the file (something like File.Save(ImagePath) etc. but it's a start.

其中ImagePath是您要保存文件路径的数据库中的实际列。这没有保存文件的代码(类似File.Save(ImagePath)等),但它是一个开始。

#5


I've never done it with Linq, but we used a b64 conversion for the image, then a clob datatype. Then reverse the b64 when you want to view the image.

我从来没有用Linq做过,但我们使用b64转换为图像,然后使用clob数据类型。当您想要查看图像时,请反转b64。