由于服务端是第三方系统的,数据库竟然是西欧字符集的,
那边开放了一个用户名,用来查询某个表的数据,
这样服务端与客户端的字符集就不一致了,导致中文乱码。
数据库服务器:AMERICAN_AMERICA.WE8ISO8859P1
客户端操作系统是简体中文
1、用在客户端sqlplus直接连接
C:\Users\Administrator>sqlplus scott/tiger@ORCL
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 9月 17 17:24:43 2014
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
SQL> select name from tab_test where rownum<3;
NAME
-------------------------------------------------------------------------------
?í?eoé
?ó???·
--直接就是中文乱码
2、通过C#程序采用OleDb的方式查询出来也是乱码
Provider=OraOLEDB.Oracle;Password=tiger;Persist Security Info=True;User ID=scott;Data Source=ORCL;
3、在sqlplus连接之前设置当前回话的环境变量
C:\Users\Administrator>set nls_lang=AMERICAN_AMERICA.WE8ISO8859P1
C:\Users\Administrator>sqlplus scott/tiger@ORCL
SQL*Plus: Release 11.2.0.1.0 Production on Wed Sep 17 17:29:32 2014
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
SQL> select name from tab_test where rownum<3;
NAME
--------------------------------------------------
张三
李四
SQL>
4、在程序种设置环境变量
既然通过设置sqlplus 的环境变量能够解决中文乱码,那么设置程序的环境变量呢?
//环境变量 设置 System.Environment.SetEnvironmentVariable("nls_lang", "AMERICAN_AMERICA.WE8ISO8859P1");
System.Environment.SetEnvironmentVariable("nls_lang", "SIMPLIFIED CHINESE_CHINA.WE8ISO8859P1", EnvironmentVariableTarget.Process); string sWhere ="select name from tab_test where rownum<3"; DataTable dt = OleDbHelper.ExecuteDataTable(txtConStr.Text.Trim(), CommandType.Text, sWhere);
修改注册表:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session\Manager\Environment
添加:nls_lang=AMERICAN_AMERICA.WE8ISO8859P1
也可以在windows环境变量 设置
set nls_lang=SIMPLIFIED CHINESE_CHINA.ZHS16GBK
set nls_lang=AMERICAN_AMERICA.WE8ISO8859P1