0

I post this question has followup of This question, since the thread is not recieving more answers.

I'm trying to understand if it is possible to pass as a parameter of a CLR stored procedure a large amount of data as "0x5352532F...".

This is to avoid to send the data directly to the CLR stored procedure, instead of sending ti to a temporary DB field and from there passing it as varbinary(max) parmeter to the CLR stored procedure.

I have a triple question:

1) is it possible, if yes how? Let's say i want to pass a pdf file to the CLR stored procedure (not the path, the full bits that make up the file). Something like:

exec MyCLRStoredProcs.dbo.insertfile
@file_remote_path ='c:\temp\test_file.txt' ,
@file_contents=0x4D5A90000300000004000.... --(this long list is the file content)

where insertfile is a stored proc that writes to the server path (at file_remote_path) the binary data I pass as (file_contents).

2) is it there corruption risk of adopting this approach (or it is the same approach that sql server uses behind the scenes)?

3) how to convert the content of a file into the "0x23423..." hexadecimal representation

4

1 回答 1

2

你的目标是什么?您是否尝试将文件从客户端文件系统传输到服务器文件系统?如果是这样,您可能需要查看 Web 服务文件传输机制。

是否要将数据持久化到数据库中?如果是这样,并且您可以访问 SQL Server 2008,我建议您查看新的FILESTREAM类型。这种类型为您维护数据库和文件系统之间的链接。

或者,如果您没有 SQL Server 2008,您可以选择将其保存为文件并在数据库中维护其字符串路径或将文件的内容存储在VARBINARY(MAX)列中。

如果您只想将数据放入数据库,则不需要 CLR proc。您可以将其直接保存到数据库中,也可以编写一个 SQL 存储过程来执行此操作。

假设您保留将其发送到 CLR proc 的方法:

1)有可能吗,如果是的话怎么办?

当然,为什么不呢。您编写的代码看起来是一个很好的例子。存储过程需要将字符串转换为字节。

2) 采用这种方法是否存在腐败风险

我不确定你在这里的意思。SQL Server 会随机替换字符串中的字符吗?不,你可能会不小心达到某种限制吗?是的,可能;的最大大小NVARCHAR(MAX)为 2^31-1,即 2,147,483,647 个字符。但我怀疑你会有这么大的 PDF。您可能会丢失磁盘上的文件和它的数据库路径之间的链接吗?是的,虽然FILESTREAM应该为你处理好。

3)如何将文件内容转换为“0x23423...”的十六进制表示

互联网上有很多关于如何做到这一点的例子。这是一个:

如何将字节数组转换为十六进制字符串,反之亦然?

于 2010-05-07T13:50:16.873 回答