How to pass nvarchar (non-English) to CLR (C#) stored procedure?

空扰寡人 提交于 2019-12-09 06:08:28

No example code has been provided (the C# or even the T-SQL to call the Stored Procedure) so it is hard to say for certain, but you are probably making a mistake in how you are calling the Stored Procedure. Most likely you are not prefixing the Unicode and XML literals with an N to keep them as Unicode. The following C# code and T-SQL show that SQLCLR does properly accept and display Unicode characters across all three datatypes: SqlString, SqlChars, and SqlXml (sample Hebrew and Russian text taken from your related Question):

C#

using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public class UnicodeTesting
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void PrintTest(SqlString StringParam, SqlChars CharsParam,
                                 SqlXml XmlParam)
    {
        SqlContext.Pipe.Send("Unicode test: blip");
        SqlContext.Pipe.Send("Unicode test: בליפ");
        SqlContext.Pipe.Send("Unicode test: Блип");

        SqlContext.Pipe.Send("StringParam: " + StringParam.Value);
        SqlContext.Pipe.Send("CharsParam: " + new String(CharsParam.Value));
        SqlContext.Pipe.Send("XmlParam: " + XmlParam.Value);
    }
}

T-SQL

EXEC dbo.PrintTest N'בליפ a',
                   N'בליפ b',
                   N'<test>בליפ c</test><test>Блип</test>';

Output (in "Messages" tab)

Unicode test: blip
Unicode test: בליפ
Unicode test: Блип
StringParam: בליפ a
CharsParam: בליפ b
XmlParam: <test>בליפ c</test><test>Блип</test>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!