如何使用 sql server management studio 将 blob 插入数据库

发布于 2024-08-09 19:21:37 字数 152 浏览 2 评论 0原文

如何轻松地将 blob 插入 varbinary(MAX) 字段?

举个例子:

我要插入的是:c:\picture.png
该表是 mytable
该列是 mypictureblob
这个地方是recid=1

How can I easily insert a blob into a varbinary(MAX) field?

As an example:

thing I want to insert is: c:\picture.png
the table is mytable
the column is mypictureblob
the place is recid=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

娜些时光,永不杰束 2024-08-16 19:21:38

好吧...这花了我太长时间。 sql-management studio工具只是不能完成像这样的简单事情(我之前在寻找在哪里设置查询超时时注意到这一点,并且它是在4个不同的位置完成的)

我下载了一些其他sql编辑器包(在我的例子中是 sql maestro)。看哪,它包括一个 Blob 编辑器,您可以在其中查看 Blob,并将新的 Blob 加载到这些字段中。

感谢您的投入!

Ok... this took me way too long. The sql-management studio tool is just not up to simple things like this (which I've noticed before when looking for where to set the timeout on queries, and it was done in 4 different locations)

I downloaded some other sql editor package (sql maestro in my case). And behold it includes a blob editor where you can look at blobs, and load new blobs into these field.

thanks for the input!

梦罢 2024-08-16 19:21:37

您可以在 SQL Server Management Studio 中使用 T-SQL(特别是使用 OPENROWSET 命令)插入 varbinary(max) 字段。

例如:

INSERT Production.ProductPhoto 
(
    ThumbnailPhoto, 
    ThumbnailPhotoFilePath, 
    LargePhoto, 
    LargePhotoFilePath
)
SELECT ThumbnailPhoto.*, null, null, N'tricycle_pink.gif'
FROM OPENROWSET 
    (BULK 'c:\images\tricycle.jpg', SINGLE_BLOB) ThumbnailPhoto

请查看以下文档,了解一个很好的示例/演练

使用大值类型

请注意,本例中的文件路径是相对于目标 SQL 服务器的,而不是相对于运行此命令的客户端。

You can insert into a varbinary(max) field using T-SQL within SQL Server Management Studio and in particular using the OPENROWSET commmand.

For example:

INSERT Production.ProductPhoto 
(
    ThumbnailPhoto, 
    ThumbnailPhotoFilePath, 
    LargePhoto, 
    LargePhotoFilePath
)
SELECT ThumbnailPhoto.*, null, null, N'tricycle_pink.gif'
FROM OPENROWSET 
    (BULK 'c:\images\tricycle.jpg', SINGLE_BLOB) ThumbnailPhoto

Take a look at the following documentation for a good example/walkthrough

Working With Large Value Types

Note that the file path in this case is relative to the targeted SQL server and not your client running this command.

嗫嚅 2024-08-16 19:21:37

MSDN 有一篇文章使用大值类型,
它试图解释导入部分是如何工作的,但它可能会有点令人困惑,因为它同时做两件事。

我在这里提供一个简化版本,分为两部分。假设有以下简单表:

CREATE TABLE [Thumbnail](
   [Id]        [int] IDENTITY(1,1) NOT NULL,
   [Data]      [varbinary](max) NULL
CONSTRAINT [PK_Thumbnail] PRIMARY KEY CLUSTERED 
(
[Id] ASC
) ) ON [PRIMARY]

如果您运行(在 SSMS 中):

SELECT * FROM OPENROWSET (BULK 'C:\Test\TestPic1.jpg', SINGLE_BLOB) AS X

它将显示结果看起来像一个表,其中有一列名为 BulkColumn。这就是为什么您可以在 INSERT 中使用它,如下所示:

INSERT [Thumbnail] ( Data )
SELECT * FROM OPENROWSET (BULK 'C:\Test\TestPic1.jpg', SINGLE_BLOB) AS X

剩下的只是将其放入具有更多列的插入中,您的表可能有也可能没有。如果您命名 select FOO 的结果,那么您可以在表中其他字段的常量之后使用 SELECT Foo.BulkColumnas

更棘手的部分是如何将该数据导出回文件中,以便您可以检查它是否仍然正常。如果你在命令行上运行它:

bcp "select Data from B2B.dbo.Thumbnail where Id=1" 
queryout D:\T\TestImage1_out2.dds -T -L 1 

它将开始抱怨 4 个额外的“参数”,并给出误导性的默认值(这将导致文件发生变化)。您可以接受第一个,将第二个设置为 0,然后接受第三个和第四个,或者明确地说:

输入字段Data的文件存储类型[varbinary(max)]:
输入字段数据 [8] 的前缀长度:0
输入字段数据长度 [0]:
输入字段终止符[无]:

然后会询问:

您想将此格式信息保存在文件中吗? [是/否] y
主机文件名 [bcp.fmt]:C:\Test\bcp_2.fmt

下次运行它时添加 -f C:\Test\bcp_2.fmt 它将停止抱怨: -)
节省大量时间和悲伤。

MSDN has an article Working With Large Value Types,
which tries to explain how the import parts work, but it can get a bit confusing since it does 2 things simultaneously.

Here I am providing a simplified version, broken into 2 parts. Assume the following simple table:

CREATE TABLE [Thumbnail](
   [Id]        [int] IDENTITY(1,1) NOT NULL,
   [Data]      [varbinary](max) NULL
CONSTRAINT [PK_Thumbnail] PRIMARY KEY CLUSTERED 
(
[Id] ASC
) ) ON [PRIMARY]

If you run (in SSMS):

SELECT * FROM OPENROWSET (BULK 'C:\Test\TestPic1.jpg', SINGLE_BLOB) AS X

it will show, that the result looks like a table with one column named BulkColumn. That's why you can use it in INSERT like:

INSERT [Thumbnail] ( Data )
SELECT * FROM OPENROWSET (BULK 'C:\Test\TestPic1.jpg', SINGLE_BLOB) AS X

The rest is just fitting it into an insert with more columns, which your table may or may not have. If you name the result of that select FOO then you can use SELECT Foo.BulkColumn and as after that constants for other fields in your table.

The part that can get more tricky is how to export that data back into a file so you can check that it's still OK. If you run it on cmd line:

bcp "select Data from B2B.dbo.Thumbnail where Id=1" 
queryout D:\T\TestImage1_out2.dds -T -L 1 

It's going to start whining for 4 additional "params" and will give misleading defaults (which will result in a changed file). You can accept the first one, set the 2nd to 0 and then assept 3rd and 4th, or to be explicit:

Enter the file storage type of field Data [varbinary(max)]:
Enter prefix-length of field Data [8]: 0
Enter length of field Data [0]:
Enter field terminator [none]:

Then it will ask:

Do you want to save this format information in a file? [Y/n] y
Host filename [bcp.fmt]: C:\Test\bcp_2.fmt

Next time you have to run it add -f C:\Test\bcp_2.fmt and it will stop whining :-)
Saves a lot of time and grief.

陌伤浅笑 2024-08-16 19:21:37

使用 TSQL SELECT BLOB 有两种方法:

SELECT * FROM OPENROWSET (BULK 'C:\Test\Test1.pdf', SINGLE_BLOB) a

以及:

SELECT BulkColumn FROM OPENROWSET (BULK 'C:\Test\Test1.pdf', SINGLE_BLOB) a

注意 FROM 子句后面的相关名称,这是强制性的。

然后您可以通过执行 INSERT SELECT 将其插入。

您还可以使用第二个版本进行更新,如我在 如何使用 TSQL 更新 SQL SERVER 中的 BLOB

There are two ways to SELECT a BLOB with TSQL:

SELECT * FROM OPENROWSET (BULK 'C:\Test\Test1.pdf', SINGLE_BLOB) a

As well as:

SELECT BulkColumn FROM OPENROWSET (BULK 'C:\Test\Test1.pdf', SINGLE_BLOB) a

Note the correlation name after the FROM clause, which is mandatory.

You can then this to INSERT by doing an INSERT SELECT.

You can also use the second version to do an UPDATE as I described in How To Update A BLOB In SQL SERVER Using TSQL .

我为君王 2024-08-16 19:21:37

但是,您可以简单地从 SQL Server 计算机上的磁盘读取文件:

select * from openrowset (bulk 'c:\path\filename.ext',single_blob) a

以十六进制形式在管理应用程序 (Management Studio) 中查看它。

因此,您可以例如将数据库备份到文件(服务器本地),然后通过上面的语句将其下载到其他地方。

However you can simply read a file from disk on SQL server machine:

select * from openrowset (bulk 'c:\path\filename.ext',single_blob) a

to see it in management application in hex form (Management Studio).

So, you can, for example, backup database to file (locally on server) and then download it to other place by the statement above.

意中人 2024-08-16 19:21:37

你需要从 mgmt studio 来做吗?下面是我们从 cmd 行执行此操作的方法:

“C:\Program Files\Microsoft SQL Server\MSSQL\Binn\TEXTCOPY.exe” /S <服务器> /D <数据库> /T mytable /C mypictureblob /F "C:\picture.png" /W"where RecId=" /I

Do you need to do it from mgmt studio? Here's how we do it from cmd line:

"C:\Program Files\Microsoft SQL Server\MSSQL\Binn\TEXTCOPY.exe" /S < Server> /D < DataBase> /T mytable /C mypictureblob /F "C:\picture.png" /W"where RecId=" /I

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文