How to make SqlContext.Pipe.Send in SQLCLR stored proc work with unicode?

…衆ロ難τιáo~ 提交于 2019-12-13 19:19:36

问题


I'm having problems with SQL CLR integration with unicode (Hebrew and Russian text). As a test, I made a simple database project in Visual Studio with the following CLR stored procedure:

[Microsoft.SqlServer.Server.SqlProcedure]
public static void TestProc ()
{
    //Console.OutputEncoding = System.Text.Encoding.Unicode;
    Trace.WriteLine("Unicode test: blip");
    Trace.WriteLine("Unicode test: בליפ");
    Trace.WriteLine("Unicode test: Блип");
    SqlContext.Pipe.Send("Unicode test: blip");
    SqlContext.Pipe.Send("Unicode test: בליפ");
    SqlContext.Pipe.Send("Unicode test: Блип");
}

Published to a test database and executed the stored procedure in SQL server:

EXEC dbo.TestProc;

Both the trace output and SQL server messages replace non-English characters with '????'.

I thought both SQL server and C# worked with unicode. Working with unicode in a Windows Forms project, for example by adding non-English text to a textbox, works fine.

How can I fix this? Is there some setting I'm missing?

This Question relates to the following: How to pass nvarchar (non-English) to CLR (C#) stored procedure?


回答1:


I am guessing that while doing this test, you never closed and re-opened the tab containing the .cs file. Is that correct? You see, Visual Studio doesn't compile the code in the editor, it compiles the code that is saved in the file that you might be currently editing. Those are usually 100% the same, BUT when it comes to "characters" based on values above 127, then Code Pages / Encodings become important. And to be clear, we are talking about the Encoding / Code Page of the C# file, not of the Database.

The problem is that the default encoding, at least on my system and I suspect on most within the United States, is "Windows 1252". So what is happening is that when you compile this code into the Assembly, what is in your editor is saved to disk. But the file encoding cannot support those characters, hence they get turned into ? in the C# file ! Yes, the code getting compiled is not what you are seeing on screen. But you would see it if you were to close the .cs file and re-open it. Both the Hebrew and Russian text would be all ?s. And that is what SQL Server is being asked to print.

To fix this, just go to the File menu, select Advanced Save Options... and in the top drop-down for Encoding, select either:

  • Unicode (UTF-8 with signature) - Codepage 65001

    or:

  • Unicode - Codepage 1200 [ this one should save as UTF-16 Little Endian ]

Replace the ? with the intended Hebrew and Russian characters. Now you can Build and Publish and have those characters display as intended.



来源:https://stackoverflow.com/questions/36965984/how-to-make-sqlcontext-pipe-send-in-sqlclr-stored-proc-work-with-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!