c# - An item with the same key has already been added in Dictionary -
i working on sql clr application, after integrating sql, getting error (for english it's working problems while adding unicodes dictionory):
**msg 6522, level 16, state 2, line 1 .net framework error occurred during execution of user-defined routine or aggregate "sqlcompare": system.argumentexception: item same key has been added. system.argumentexception: @ system.throwhelper.throwargumentexception(exceptionresource resource) @ system.collections.generic.dictionary`2.insert(tkey key, tvalue value, boolean add) @ system.collections.generic.dictionary`2.add(tkey key, tvalue value) @ translate_class_num..ctor() @ stringpercentagecompare..ctor() @ userdefinedfunctions.sqlcomparison()** here code in c# private dictionary<string, string> mydictionary; //private string[,] myarray; public translate_class_num() { mydictionary.add("?", "01"); mydictionary.add("?", "02"); mydictionary.add("?", "03"); mydictionary.add("?", "04"); mydictionary.add("?", "05") }
and in sql server code create assembly dbdb_deduplication authorization dbo 'e:\projects\dbdb_deduplication\dbdb_deduplication\obj\debug\dbdb_deduplication.dll' permission_set = safe go
create function sqlcompare() returns nvarchar(50) external name dbdb_deduplication.userdefinedfunctions.sqlcomparison; go
select dbo.sqlcompare(); go
thanks in advance
this code works fine me:
dictionary<string, string> dict = new dictionary<string, string>(); dict.add("ಅ","01"); dict.add("ಇ","02");
as tried explain before in comments, unicode string must not passing through compiler in way expect. can due c# source file being saved ascii encoding or other non-unicode supporting encoding. guarantee results, use c# unicode literal encoding (u+0000 u+ffff).
if don't know how equivalent unicode escape sequence, can use code:
static string encodenonasciicharacters( string value ) { stringbuilder sb = new stringbuilder(); foreach( char c in value ) { if( c > 127 ) { // character big ascii string encodedvalue = "\\u" + ((int) c).tostring( "x4" ); sb.append( encodedvalue ); } else { sb.append( c ); } } return sb.tostring(); } console.writeline(encodenonasciicharacters("ಅ")); \u0c85 console.writeline(encodenonasciicharacters("ಇ")); \u0c87
so can write safer version of source code as:
dictionary<string, string> dict = new dictionary<string, string>(); dict.add("\u0c85","01"); dict.add("\u0c87","02");
Comments
Post a Comment