visual-studio 插入字节数组 INTO varbinary(max) 记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2214628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:31:14  来源:igfitidea点击:

Insert Bytes array INTO varbinary(max) record

sqlvisual-studiosql-server-2008

提问by Lefteris Gkinis

I want to INSERT INTO a table in a record varbinary(max) a Byte array How can I do that?

我想在记录中插入一个表 varbinary(max) 一个字节数组我该怎么做?

采纳答案by Ray

Using a stored procedure, just create a parameter of type varbinary(max) and insert it into the table as you would any data type.

使用存储过程,只需创建一个 varbinary(max) 类型的参数并将其插入到表中,就像插入任何数据类型一样。

In your c# (or vb or whatever) code, add a parameter to your sql command object and set the byte array as the parameter value:

在您的 c#(或 vb 或其他)代码中,向您的 sql 命令对象添加一个参数并将字节数组设置为参数值:

command.Parameters.AddWithValue("@parameter_name", myByteArray);

If not using a stored procedure, you can probably do the same with a parameterized sql statement, but I have never tried that, so I can't give an example.

如果不使用存储过程,你可能可以用参数化的 sql 语句做同样的事情,但我从未尝试过,所以我不能举个例子。

Edit:

编辑:

You are using a parameterized query, which is not my thing, so I can't guarantee this will work. But, here is some code that should get you going.

您正在使用参数化查询,这不是我的事,所以我不能保证这会起作用。但是,这里有一些代码应该可以帮助您前进。

RemoteSQLcmd = New SqlCommand("INSERT INTO Table(1) Values (newid(), ProductID, @bin_value", RemoteSQLConn)
RemoteSQLcmd.Parameters.AddWithValue(@bin_value", imSource) ;

The binary value must be represented as a parameter (@bin_value), and the value is set by the AddWithValue statement. The parameter name does not have to match the column name.

二进制值必须表示为参数(@bin_value),该值由 AddWithValue 语句设置。参数名称不必与列名称匹配。

A couple of notes: I would suggest using columns names in your insert statement rather than depending on column position, Also, I don't know what you mean by 'table(1)' - is that actually the name of the table?

一些注意事项:我建议在您的插入语句中使用列名,而不是根据列位置,另外,我不知道您所说的“table(1)”是什么意思——这实际上是表的名称吗?

回答by OMG Ponies

Assuming:

假设:

CREATE TABLE [dbo].[varb_table] (
  [ID] [int] IDENTITY(1,1) NOT NULL,
  [BinaryData] [varbinary](max) NULL,
  CONSTRAINT [PK_varb_table] PRIMARY KEY CLUSTERED 
)

Use:

利用:

INSERT INTO varb_table 
  (ID, BinaryData)
VALUES 
  (NULL, 0x);

DECLARE @pk
SET @pk = @@IDENTITY;

UPDATE varb_table
   SET BinaryData.Write (@your_byte_array, 0, DATALENGTH(BinaryData))
 WHERE ID = @pk