简体   繁体   English

使用 node.js 从 SQL 服务器读取生僻字

[英]Use node.js to read the rare Chinese word from SQL Server

I encounter a problem when I tried to read the data from a ERP database.当我尝试从 ERP 数据库读取数据时遇到问题。 There is a data called '鉫承工程' and it save in SQL Server like ' 承工程'(data type is varchar).有一个名为'鉫承工程'的数据,它像'承工程'一样保存在SQL服务器(数据类型为varchar)。 The ERP System can show the correct word as '鉫承工程'. ERP 系统可以将正确的单词显示为“鉫承工程”。

However, I use node.js to read the data and it shows me the garbled text like '�r承工程'.但是,我使用 node.js 读取数据,它向我显示像'r承工程'这样的乱码。 How can I get the correct word without changing the data in database?如何在不更改数据库中的数据的情况下获得正确的单词?

Have a read through the Constants (Transact-SQL) documentation with respect to Character string constants:阅读有关字符串常量的常量 (Transact-SQL)文档:

Character string constants are enclosed in single quotation marks and include alphanumeric characters (az, AZ, and 0-9) and special characters, such as exclamation point (,), at sign (@).字符串常量用单引号括起来,包括字母数字字符(az、AZ 和 0-9)和特殊字符,例如感叹号 (,)、符号 (@)。 and number sign (#).和数字符号 (#)。 Character string constants are assigned the default collation of the current database, If the COLLATE clause is used.如果使用了 COLLATE 子句,则字符串常量被分配当前数据库的默认排序规则。 the conversion to the database default code page still happens before the conversion to the collation specified by the COLLATE clause.转换为数据库默认代码页仍然发生在转换为 COLLATE 子句指定的排序规则之前。 Character strings typed by users are evaluated through the code page of the computer and are translated to the database default code page if it is required.用户键入的字符串通过计算机的代码页进行评估,并在需要时转换为数据库默认代码页。

The key statement here is:这里的关键声明是:

Character string constants are assigned the default collation of the current database.字符串常量被赋予当前数据库的默认排序规则。

The implication here is that, when a column's collation is different than the database's default collation, using regular character string constants to insert values containing international characters can cause loss of information (ie: corrupted characters) as the literal is first interpreted using the database's default collation and is then converted to the column's collation before storage.这里的含义是,当列的排序规则不同于数据库的默认排序规则时,使用常规字符串常量插入包含国际字符的值可能会导致信息丢失(即:损坏的字符),因为首先使用数据库的默认值解释文字排序规则,然后在存储之前转换为列的排序规则。

When this happens character corruption is usually evident with unconvertible characters getting replaced by the question mark ( ? ) character.当发生这种情况时,字符损坏通常很明显,不可转换的字符被问号 ( ? ) 字符替换。

Consider the following examples:考虑以下示例:

use master;
-- Typical default collation on systems using en-US...
create database Z_Demo1 collate SQL_Latin1_General_CP1_CI_AS;
create database Z_Demo2 collate Chinese_PRC_CI_AS;
go
use Z_Demo1;
go
create table dbo.Demo (
  ID int not null identity(1,1),
  [鉫承工程] varchar(50) collate Chinese_PRC_CI_AS
);
insert dbo.Demo ([鉫承工程]) values ('承工程');
insert dbo.Demo ([鉫承工程]) values (N'承工程');
select * from dbo.Demo;
go
use Z_Demo2;
go
create table dbo.Demo (
  ID int not null identity(1,1),
  [鉫承工程] varchar(50) collate Chinese_PRC_CI_AS
);
insert dbo.Demo ([鉫承工程]) values ('承工程');
insert dbo.Demo ([鉫承工程]) values (N'承工程');
select * from dbo.Demo;
go
use master;
go
drop database if exists Z_Demo1;
drop database if exists Z_Demo2;
go

Which outputs the following result sets:输出以下结果集:

ID ID 鉫承工程鉫承工程
1 1个 ??? ???
2 2个 承工程承接工程
ID ID 鉫承工程鉫承工程
1 1个 承工程承接工程
2 2个 承工程承接工程

The first demo failed to insert both values correctly because the column's collation, Chinese_PRC_CI_AS , was different than the database's default collation, SQL_Latin1_General_CP1_CI_AS .第一个演示未能正确插入两个值,因为列的排序Chinese_PRC_CI_AS与数据库的默认排序SQL_Latin1_General_CP1_CI_AS不同。

You can avoid this issue of differing collations by using National Language string constants which are prefixed with an uppercase N character, or from application code specify the column type as NVARCHAR(...length...) instead of VARCHAR(...length...) .您可以通过使用以大写N字符为前缀的国家语言字符串常量来避免这种不同排序规则的问题,或者从应用程序代码中将列类型指定为NVARCHAR(...length...)而不是VARCHAR(...length...) Using the above tables from NodeJS using the mssql module, for example, you would specify the data types as sql.NVarChar(50) , eg: request.input("鉫承工程", sql.NVarChar(50), "承工程") .例如,使用 mssql 模块从 NodeJS 中使用上述表格,您可以将数据类型指定为sql.NVarChar(50) ,例如: request.input("鉫承工程", sql.NVarChar(50), "承工程")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM