Technology Sharing

Solve the problem of Chinese garbled characters when reading US7ASCII character set Oracle database in C#

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

👨 作者简介:大家好,我是Taro,全栈领域创作者
✒️ Personal homepage:Don Juan Taro
🚀 支持我:点赞👍+📝 评论 + ⭐️收藏



Preface

Recently, I was trying to connect to the hospital's HIS system and found that the hospital used Oracle'sAMERICAN_AMERICA.US7ASCIIThe encoding format is encoded, resulting in garbled Chinese data read into the program

insert image description here


以下是本篇文章正文内容,下面案例可供参考

1. Solution

Because the hospital unit data encoding cannot be changed, we tested many methods and finally chose a more efficient processing method (ODAC + OleDbConnection) using an independent Oracle driver to process

2. Install the System.Data.OleDb connection library

  1. Oracle.ManagedDataAccess.Client It is the most used package for connecting C# to Oracle database, and it is also the .NET data access library officially provided by Oracle, but it cannot solve the garbled problem

  2. Must adoptSystem.Data.OleDb , it uses an independent oracle driver to process, which is the key to solving the Chinese garbled code
    insert image description here

System.Data.OleDb Dependencies:
insert image description here

Core code:

  1. Database connection string:
<add connectionString="Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User ID=db;Password=db;" name="HIS" />
  • 1

Program call:
insert image description here

3. Local installation of ODAC component driver

下载ODAC :https://www.oracle.com/database/technologies/dotnet-utilsoft-downloads.html

After installing ODAC, you must restart your computer
insert image description here
insert image description here
Keep clicking "Next" until the installation is complete.
insert image description here

Please add a description of the image

4. Edit system environment variables

This step is to keep the local and client database character sets consistent, to prevent garbled characters when viewing data locally

变量名:NLS_LANG |  变量值:AMERICAN_AMERICA.US7ASCII
  • 1

The code is as follows (example):
insert image description here

5. Debugging Program

  • The Chinese data part is displayed normally, and no garbled characters are displayed.
    insert image description here

Summarize

The above is what I want to talk about today. Due to historical reasons, the early Oracle did not have a Chinese character set (such as Oracle6, Oracle7, Oracle7.1), but some users have used the database since then and stored Chinese with the US7ASCII character set. This article only briefly introduces the use of the System.Data.OleDb library, which perfectly solves the Chinese garbled problem of the US7ASCII character set Oracle database.