0

我有一个数据库,它需要以各种语言和字母保存数据。我使用默认的拉丁语排序规则,但所有文本字段都是 Unicode 种类(nchar、nvarchar)。

我可以使用我的前端应用程序愉快地从数据库中插入和检索 Unicode 数据,但是如果我使用 SSMS 查看数据,那么我看到的只是乱码!

我可以插入这个:

극단적으로

然后在 SSMS 中看起来像这样:

극단ì ìœ¼ë¡œ

但是由我的前端应用程序检索为:

극단적으로

现在显然数据本身存储正常,但为什么 SSMS 将其显示为乱码?有趣的是,如果我使用 SSMS 直接编辑数据并将其粘贴在上面,然后将其显示为这样(编辑:结果将结果从网格切换到文本会减轻我的这部分问题,因为显示的是正确的文本而不是框.):

□□□□□

但是,如果我将其复制并粘贴到文本编辑器中,它会显示为:

극단적으로

为了查看这是否只是 SSMS 行为不端(/我配置错误),我使用了 MS Access 并将其链接到我的 SQL Server 数据库,但它显示的乱码与 SQL Server 相同。

该数据库将保存 Web 应用程序的所有静态文本,因此能够轻松查看和编辑数据对我来说很重要,当您看到的只有以下内容时不容易做到:

극단ì ìœ¼ë¡œ

任何建议都会很棒,我真的坚持这一点。

编辑

这是我目前在 PHP 中完成的数据插入示例:

$con = sqlsrv_connect("(local)", array("Database" => "myDatabase"));
$SQL = "INSERT INTO TableName (col1,col2) VALUES (N'극단적으로',N'극단적으로')";
sqlsrv_query($con,$SQL);

这是我的数据检索示例:

$SQL = "SELECT col1,col2 FROM TableName";
$rs = sqlsrv_query($con,$SQL);
while($row = sqlsrv_fetch_array($rs)){
    echo $row["col1"]." ".$row["col2"]."\n";
}

这导致 PHP 输出:

극단적으로 극단적으로

但是,如果我在该表上运行选择,则数据在 SSMS 的输出窗口中显示为乱码。

4

3 回答 3

4

所以看起来你在 PHP 文件中有一个字符串文字:

$SQL = "INSERT INTO TableName (col1,col2) VALUES (N'극단적으로',N'극단적으로')";

看看这个帖子


以下是您可能需要了解的一些事项:

  1. 为 Debian 安装 mssql 支持(Lenny/Squeeze):

    apt-get install php5-sybase 
    
  2. 当您收到此错误消息时:“无法使用 DB-Library(如 ISQL)或 ODBC 版本 3.7 或更早版本将仅 Unicode 排序规则中的 Unicode 数据或 ntext 数据发送到客户端。”

    在 /etc/freetds/freetds.conf 添加这两行(最后两行):

    [global]
    ;tds version = 4.2
    tds version = 8.0
    client charset = UTF-8
    

    您也可以在 php.ini 中编辑“charset”(但如果您之前在 freetds.conf 中做过,则不需要):

    ; Specify client character set..
    ; If empty or not set the client charset from freetds.comf is used
    ; This is only used when compiled with FreeTDS
    mssql.charset = "UTF-8"
    
  3. 如果您需要 unicode 支持,请使用 nchar/nvarchar/ntext 列类型。

于 2012-06-08T10:58:15.370 回答
2

I know this is an old question, but I could see no resolution to the problem, I've just started encountering the same problem in SSMS. I don't believe that the issue is with the PHP encoding, based on the edit regarding that if the results are viewed as text instead of grid then the problem is resolved (I experience the same behavior). I think that the issue is actually in SSMS and the encoding of the grid output, given that even SSMS can correctly display the Korean characters when viewed as text.

I've managed to fix the issue in SSMS by changing the font to "Arial Unicode MS". However I have noticed that the issue has occured in other applications as well particularly text editor such as Notepad++ and Notepad.

The following post seems to contain a more permanent fix for the issue (however I am on a network computer without sufficient privileges to verify it).

sql server management console doesn't work with multi-byte characters

Note: I have been using Korean/Hangul data in SQL Server for the past 6 weeks without encountering this issue. It has only just occurred today, I have had windows updates but no other reason for the change in behavior. I have also used this viewed/queried this data previously with no issues on tables that have not been updated and are definitely encoded to NVARCHAR. My Korean colleagues can still view the data correctly in the Grid output in SSMS.

I hope this helps anyone else who stumbles upon this question.

于 2013-05-02T05:43:48.067 回答
1
于 2012-06-08T11:09:47.743 回答