【发布时间】:2016-05-26 22:43:36
【问题描述】:
在这个项目中,有一个封装了 ADO.NET 的类,用于常见的数据访问,如 ExecuteDataReader、ExecuteScalar 等。
当使用这些方法调用存储过程时,它允许您传递一个Dictionary<string, string> 的参数(字符串键、字符串值),然后将它们作为SqlParameter 添加到SqlCommand 对象中。
有一种情况是我们必须在数据库中保存一个文档。文档是byte[],数据库中对应的列是varbinary(MAX)。
我们已经四处寻找解决方案,但所有可用的都是使用 SqlDbType.Varbinary 的示例,在这种情况下这不是一个选项。
我们最近的尝试是尝试将byte[] 转换为二进制字符串,将其作为nvarchar(max) 传递到存储过程中,然后在将其保存到Document 表时使用CONVERT(varbinary(max), @theFileBinaryString),但是,这保存损坏的文件。
C#
byte[] pDocumentBytes = ...;
string documentAsBinary = "0x" + BitConverter.ToString(pDocumentBytes).Replace("-", "");
SQL
@DocumentContentAsBinary nvarchar(max) -- This is "documentAsBinary" from C# above
DECLARE @DocumentContentVarbinary varbinary(max);
SET @DocumentContentVarbinary = CONVERT(varbinary(max), @DocumentContentAsBinary);
【问题讨论】:
标签: c# sql-server stored-procedures varbinary