Kovpaev Alexey
Kovpaev Alexey

Reputation: 1786

How can I insert an array of bytes as text in the SQL Server 2008?

I use a text file with a correct sql insert-statement for bulk loading in my test project. I apply this statement to an empty table to load data. But I faced with the problem of writing an array of bytes.

How can I write the correct insert-statement that I used to pass an array of bytes? Will understand SQL Server 2008 an array of bytes as hex-string?

Added

I need some string that I can apply to SQL Server to insert an array of bytes. Something like that:

INSERT INTO T(SomeBlobColumn) VALUES (0x666F6F)

or

INSERT INTO T(SomeBlobColumn) VALUES (CAST('666F6F' AS BINARY))

Upvotes: 1

Views: 403

Answers (1)

Damyan Bogoev
Damyan Bogoev

Reputation: 696

You could try this approach (in c#):

using(SqlCommand cmd = new SqlCommand("INSERT INTO TableName(BinaryColumn) VALUES (@InputParameter)", sqlConnection))
{
    cmd.Parameters.Add("@InputParameter", SqlDbType.VarBinary, 8000).Value = byteArray;
    cmd.ExecuteNonQuery();
}

Upvotes: 2

Related Questions