如何将多个图像上传到SQL Server

介绍 (Introduction)

Sometimes we need to store information including photos in our database. For example, the photo of the product, the photo of the team members. But how can we store images in SQL Server?

有时我们需要在数据库中存储包括照片在内的信息。 例如,产品的照片,团队成员的照片。 但是我们如何在SQL Server中存储图像?

We could create an application in .NET or Java, but if we do not have experience in those programming languages, we could use SQL Server tools to do it.

我们可以使用.NET或Java创建应用程序,但是如果我们没有使用这些编程语言的经验,则可以使用SQL Server工具来完成。

In this new article, we will learn the following tips that will help us to work with images including how to:

在这篇新文章中,我们将学习以下技巧,这些技巧将帮助我们处理图像,包括如何:

  • insert one image into SQL Server

    将一张图片插入SQL Server
  • store multiple files into a table

    将多个文件存储到表中
  • verify that the images were inserted

    验证是否插入了图像

要求 (Requirements)

  1. SQL Server 2016 or later with SSMS installed. You can use older versions including SQL Server 2008.

    安装了SSMSSQL Server 2016或更高版本。 您可以使用旧版本,包括SQL Server 2008。
  2. SSDT 2015 installed, but earlier versions are allowed.

    已安装SSDT 2015,但允许使用早期版本。
  3. SSRS installed

    已安装SSRS

入门 (Getting started)

Insert one image into SQL Server

将一张图片插入SQL Server

We will first learn how to load 1 single image into a table in SQL Server.

我们将首先学习如何在SQL Server中将1个单一图像加载到表中。

We will load a file named 1.png that contains the image of your humble author into the table myimages:

我们将一个名为1.png的文件加载到表myimages中,该文件包含您卑微的作者的图像:

We will first create a table named myimages in SQL Server:

我们将首先在SQL Server中创建一个名为myimages的表:

CREATE TABLE myimages(id int, img varbinary(max))

This table will have an integer (int) id and the image column named img. The data type that we are going to use to store images is the varbinary(max).

该表将具有一个整数(int)id和名为img的图像列。 我们将用于存储图像的数据类型是varbinary(max)。

We will now insert an image into the table myimages:

现在,我们将图像插入表myimages中:

INSERT INTO adventureworks.dbo.myimages values (1, (SELECT * FROM OPENROWSET(BULK N'C:\img\1.png', SINGLE_BLOB) as T1))

The INSERT statement inserts the value 1 as the id and then inserts the image named 1.png from the folder img in the c drive. I am assuming that you have an image in that path. We are using the OPENROWSET to access remote data like OLE DB data sources.

INSERT语句插入值1作为id,然后从c驱动器的img文件夹中插入名为1.png的图像。 我假设您在该路径中有一个图像。 我们正在使用OPENROWSET访问诸如OLE DB数据源之类的远程数据。

We now know how to insert one single image into our database. However, how can we save all the files from a folder into our table?

现在我们知道如何将一个图像插入数据库。 但是,如何将文件夹中的所有文件保存

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值