3

I have been trying to assign/append a string to a blob column in an output buffer, on a C# script that's taking a number of input rows, and concatenating them where the related id is identical, passing onto the next row where the is new, and seem to be coming up against a problem that may be me not knowing the intermediate steps.

I'm using this:

Output0Buffer.compAlert.AddBlobData(Encoding.Unicode.GetBytes(alert),alert.Length); 

To assign the alert string to the NTEXT column compAlert.

The theory and what I can see from previous answers is that this will add the string alert to said NTEXT column. The issue I'm coming across is this only adds the first character of that string. As near as I can tell, if GetBytes is fed a string, it should iterate over that string and add everything? I appear to be missing something that all the other answers that say to use Encoding.Unicode.GetBytes() are taking for granted that I don't know that should be done?

Hadi
  • 36,233
  • 13
  • 65
  • 124
Wenlocke
  • 155
  • 4
  • Aside from anything else, `alert.Length` is the number of *characters* in the string - whereas `Encoding.Unicode.GetBytes` will return twice as many *bytes*. I suggest you call the overload that only has a single parameter (assuming I'm looking at the docs for the right AddBlobData method). – Jon Skeet Feb 21 '22 at 19:02
  • @JonSkeet there is two overloads: https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.dts.pipeline.blobcolumn.addblobdata?view=sqlserver-2019#microsoft-sqlserver-dts-pipeline-blobcolumn-addblobdata(system-byte()-system-int32) – Hadi Feb 21 '22 at 19:49
  • @Hadi: That was the page I was looking at - it just wasn't entirely clear to me based on the tags whether that was the type involved. – Jon Skeet Feb 21 '22 at 19:52

1 Answers1

2

Based on the official documentation, the count argument in the public void AddBlobData (byte[] data, int count) method refers to :

The number of bytes of binary data to be appended.

You should use Encoding.Unicode.GetBytes(alert).length instead of alert.length.

Output0Buffer.compAlert.AddBlobData(Encoding.Unicode.GetBytes(alert),Encoding.Unicode.GetBytes(alert).Length); 

Or simply use:

Output0Buffer.compAlert.AddBlobData(Encoding.Unicode.GetBytes(alert));
Hadi
  • 36,233
  • 13
  • 65
  • 124