Insert byte array INTO varbinary (max) record

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

+4
source share
2 answers

Using a stored procedure, simply create a parameter of type varbinary (max) and paste it into the table, like any data type.

In C # code (or vb or something else) add the parameter to your sql command line object and set the byte array as the parameter value:

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

If you do not use the stored procedure, you can do the same with the parameterized sql statement, but I never tried this, so I can not give an example.

Edit:

You are using a parameterized query, which is not my business, so I can not guarantee that this will work. But here is the code you have to do.

 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 must not match the column name.

A few notes: I would suggest using column names in your insert statement, and not depending on the column position. Also, I don't know what you mean by "table (1)" - is that really the name of the table?

+3
source

Assuming that:

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

Using:

 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 
+1
source

Source: https://habr.com/ru/post/1300517/


All Articles