简体   繁体   English

将存储在SQL XML参数中的byte []存储到SQL Server 2005中的varbinary(MAX)字段。可以完成吗?

[英]Store a byte[] stored in a SQL XML parameter to a varbinary(MAX) field in SQL Server 2005. Can it be done?

Store a byte[] stored in a SQL XML parameter to a varbinary(MAX) field in SQL Server 2005. Can it be done ? 将存储在SQL XML参数中的byte []存储到SQL Server 2005中的varbinary(MAX)字段。可以完成吗?

Here's my stored procedure: 这是我的存储过程:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER   PROCEDURE [dbo].[AddPerson]
 @Data AS XML
AS  
 INSERT INTO Persons (name,image_binary)
 SELECT 
  rowWals.value('./@Name', 'varchar(64)') AS [Name],
  rowWals.value('./@ImageBinary', 'varbinary(MAX)') AS [ImageBinary]
 FROM 
  @Data.nodes ('/Data/Names') as b(rowVals)

 SELECT SCOPE_IDENTITY() AS Id

In my schema Name is of type String and ImageBinary is o type byte[]. 在我的架构中,Name的类型为String,而ImageBinary的类型为byte []。 Should I use the String type for ImageBinary too ? 我也应该对ImageBinary使用String类型吗? Would I then need to specially encode that string somehow ? 然后,我是否需要以某种方式对字符串进行特殊编码?

Assuming that you use Base64 for the byte[] in XML, the approach which uses XQuery as described in the following article should work: 假设您对XML中的byte []使用Base64,则下面的文章中介绍的使用XQuery的方法应该可以工作:

http://blogs.msdn.com/sqltips/archive/2008/06/30/converting-from-base64-to-varbinary-and-vice-versa.aspx http://blogs.msdn.com/sqltips/archive/2008/06/30/converting-from-base64-to-varbinary-and-vice-versa.aspx

Probably not. 可能不是。

XML is alphanumeric (eg codes > 32 basically) where byte[] would be 0->255. XML是字母数字(例如,基本上是> 32的代码),其中byte []为0-> 255。

In this case, you have 2 sets of data: a name and a BLOB. 在这种情况下,您有2组数据:名称和BLOB。 So treat them as such, no? 所以这样对待他们,不是吗?

ALTER  PROCEDURE [dbo].[AddPerson]
 @Name AS varchar(64)
 @Data AS varbinary(max)
AS  
 INSERT INTO Persons (name, image_binary)
 VALUES (@Name, @Data)
 SELECT SCOPE_IDENTITY() AS Id
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server VARBINARY(max) 到 c# byte[] - SQL Server VARBINARY(max) to c# byte[] SQL Server VARBINARY(最大) - SQL Server VARBINARY(MAX) 如何将C#中的byte []作为字符串传递到SQL Server存储过程并转换为varbinary(MAX) - How to pass byte[] from C# as string to SQL Server stored procedure and convert into varbinary(MAX) 在SQL Server的Varbinary(MAX)或Image数据类型中存储转换为字节数组的位图 - Store Bitmap converted to byte array in Varbinary(MAX) or Image data type of SQL Server SQL Server 2005:如何在存储过程中“访问” XML表参数 - SQL Server 2005 : how to 'access' XML table parameter in stored procedure 从SQL Server中检索varbinary(MAX)到C#中的byte [] - Retrieve varbinary(MAX) from SQL Server to byte[] in C# 如何在 SQL Server 中存储和检索 varbinary(max) 列 - How to Store and Retrieve a varbinary(max) Column in SQL Server 将C#byte []传递给VARBINARY SQL字段 - Passing C# byte[] to a VARBINARY SQL field 我可以使用varbinary类型在SQL Server数据库中存储图像吗? - Can I use varbinary type to store image in SQL Server database? 如何将 Byte[] 转换为数据以在 SQL Server 的 varbinary(max) 列中插入值? - How convert a Byte[] to a data to insert a value in a varbinary(max) column in SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM