且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sql数据库中存储和检索图像

更新时间:2023-01-30 17:53:59

根据图像的大小,我不建议直接将其存储在数据库中 - 小缩略图大小还可以,但是这些天的图像变得非常快,并且可能占用大量的SQL服务器带宽和数据库空间。如果你正在考虑存储大量的大图像,我会为它们创建一个文件夹,并将它们存储在一个临时名称下,然后在数据库中记录它。就个人而言,我将拇指存放在数据库中,并提供完整图像的路径参考。



但是,存储或检索图像并不困难:

Depending on the size of the image, I wouldn't recommend storing it in the DB directly - small thumbnail sizes are ok, but images these days get big pretty fast, and can take up a lot of SQL server bandwidth and DB space. If you are looking at storing a lot of largish images, I would create a folder for them, and store them under a temporary name, recording that in the DB instead. Personally, I store a thumb in the DB, with a path reference to the full image.

However, it isn't difficult to store or retrieve images:
using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO Images (Id, Location, ImageData, InsertDate) VALUES (@ID, @DS, @TN, @DI)", con))
        {
        cmd.Parameters.AddWithValue("@ID", Id);
        cmd.Parameters.AddWithValue("@DS", Location);
        cmd.Parameters.AddWithValue("@TN", myImage.ToByteArray());
        cmd.Parameters.AddWithValue("@DI", InsertDate);
        cmd.ExecuteNonQuery();
        }
    }




using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT ImageData FROM Images WHERE Id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", Id);
        SqlDataReader r = cmd.ExecuteReader();
        if (r.Read())
            {
            MemoryStream ms = new MemoryStream((byte[])r["ImageData"]);
            MyImage = Image.FromStream(ms);
            }
        }
    }


参考:

示例程序演示如何在SQL Server中保存或存储图像

存储或在SQL Server中保存图像 [ ^ ]



本文介绍如何使用C#在Microsoft .NET中存储和检索数据库中的图像。

使用Microsoft .NET从SQL Server存储和检索图像 [ ^ ]



您也可以尝试搜索 google [ ^ ]或 CodeProject [ ^ ]
Refer:
A sample program to demonstrate how to save or store images in SQL server
Store or Save images in SQL Server[^]

This article is about storing and retrieving images from database in Microsoft .NET using C#.
Storing and Retrieving Images from SQL Server using Microsoft .NET[^]

You can also try search on google[^] or CodeProject[^]


使用'BLOB'概念来存储和检索图像。



一个很好的例子是 dotnet_read_write_blob_Example 。



查看链接,你可能会能够找到您的查询的解决方案。
use 'BLOB' concept to store and retrieve Images.

A very good example is dotnet_read_write_blob_Example .

go through the link and you might be able to find out solution for your query.