Search code examples
c#sqlunicodeencodingdynamics-crm

Is there an equivalent method in asp.net that behaves exactly as SQL function NCHAR(integer-expression)?


I am in process of importing some table values into Microsoft CRM db. During this process, if the text being imported contains certain unicode characters, CRM API fails to import the batch. My goal is to weed out the records that contain these 'illegal' characters.

To that end, I have identified the illegal characters by experimenting on CRM import. I imported each UTF-16, NCHAR(0) - NCHAR(65535), characters and made note of the ones that threw the error.

I now want to create a method in my solution to disallow inputs with the illegal characters. For that, I need to be able to compare each incoming character with the list of illegal characters. My problem is, I am not being able to get the list of illegal characters, such as NCHAR(5) in my solution. Is there a way to convert integer-expressions to UTF-16 character in C# vis-a-vis NCHAR(integer-expression) in SQL?


Solution

  • Just cast to char. In your example you have 5 and (char)5 gives you '\u0005' just as NCHAR(5) does.

    (Things are slightly different with the supplementary character flag set, which lets NCHAR cover all Unicode characters from 0 to 1114111, which requires a string representation as it can be two UTF-16 units).