轉載:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005

来源:http://www.cnblogs.com/which/archive/2016/02/26/5219637.html
-Advertisement-
Play Games

https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the code page of the client computer differs from the cod


https://support.microsoft.com/en-us/kb/904803

Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005

SYMPTOMS

Consider the following scenario:

  • In Microsoft SQL Server 2005, you use SQL Server Management Studio to query character data from a SQL Server database that uses a non-Unicode data type. For example, the SQL Server database uses the char data type, thevarchar data type, or the text data type.
  • The code page of the client computer differs from the code page of the database. The code page is associated with the collation of the database.

In this scenario, character data is represented incorrectly.
For example, you may experience one of the following problems:

  • The character data is represented as a question mark (?). You may see this problem if you inserted or updated the character data as a non-Unicode data type before you queried the character data. This problem occurs if you make this change by using SQL Server Management Studio on a client computer that has a different code page.
  • The character data is represented as corrupted data. The character data of code page X is stored in a non-Unicode column of code page Y. Additionally, the character data is not translated. This problem occurs when you query the character data by using SQL Server Management Studio.
    Note When you query the character data by using SQL Query Analyzer in Microsoft SQL Server 2000, the character data is represented correctly if the Perform translation for character data setting (the Auto Translateparameter) is disabled. The Auto Translate parameter is a parameter of the ConnectionString property for Microsoft OLE DB Provider for SQL Server and for Microsoft .NET Framework Data Provider for OLE DB.
CAUSE

This problem occurs because the character data of code page X is stored in a non-Unicode column of code page Y. Additionally, the character data is not translated correctly. We do not support storing the character data of code page Xin a column of code page Y.
In SQL Server 2005, when you use a string literal of a non-Unicode data type, the string literal is converted by using the database's default code page that is derived from the database's collation. Storing the character data of code page X in a column of code page Y may cause data loss or data corruption.
If the character data is represented as corrupted data, the data can be represented correctly only if you disable the Auto Translate parameter for Microsoft OLE DB Provider for SQL Server or for Microsoft .NET Framework Data Provider for OLE DB.
Note SQL Server Management Studio uses Microsoft .NET Framework Data Provider for SQL Server to connect to the SQL Server database. This data provider does not support the Auto Translate parameter.

WORKAROUND

To work around this problem, use one of the following methods.

Method 1: Use a Unicode data type instead of a non-Unicode data type

Change the columns to a Unicode data type to avoid all the problems that are caused by code page translation. For example, use the nchar data type, the nvarchar data type, or the ntext data type.
For more information about storing Unicode data, click the following article number to view the article in the Microsoft Knowledge Base:

239530 You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server

Method 2: Use an appropriate collation for the database

If you must use a non-Unicode data type, always make sure that the code page of the database and the code page of any non-Unicode columns can store the non-Unicode data correctly. For example, if you want to store code page 949 (Korean) character data, use a Korean collation for the database. For example, use the Korean_Wansung_CI_AS collation for the database.

Method 3: Use the binary data type or the varbinary data type

If you want the database to directly store and retrieve the exact byte values of the characters that are handled without trying to perform appropriate code page translation, use the binary data type or the varbinary data type.

Method 4: Use a different tool to store and retrieve data, and disable the Auto Translate parameter

Warning We do not test or support storing the character data of code page X in a column of code page Y. This operation may cause linguistically incorrect query results, incorrect string matching or ordering, and unexpected code page translation (data corruption). We encourage you to use one of the other methods to work around this problem.
When you use Microsoft OLE DB Provider for SQL Server to connect to a database that has a different code page and you try to query character data from a non-Unicode data type column, you must make sure that you store the untranslated characters to the database.
Note The following example assumes that the code page of the client computer is Korean (CP949) and that the code page of the SQL Server database is English (CP1252). You must replace the placeholders in the code examples with values that are appropriate for your situation.
To work around this problem, follow these steps:

  • Manually convert the characters to raw data, and then insert the data into the database by using the code page of the database. To do this, use code that is similar to the following code example.
    string strsrc="가";string strsrc="가";
    string strtag=Encoding.GetEncoding(1252).GetString(Encoding.GetEncoding(949).GetBytes (strsrc));
    sql="insert into <tablename> (<column>,) values ('" + strtag + "')";
    // code for updating the database;

    Note This code example is written in C#.

  • When you want to query the data, use Microsoft OLE DB Provider for SQL Server or Microsoft .NET Framework Data Provider for SQL Server to connect to the database, and then set the Auto Translate parameter to False. To do this, use code that is similar to the following code example.
    OleDbConnection conn=new OleDbConnection("Provider=SQLOLEDB;" +
    	" Initial Catalog =<yourdatabase>;"+
    	"User id=<youruserid>; Password=<yourpassword>;"+
    	"Auto Translate=False"
    	);
    // code for representing the character data;
    
STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION
Steps to reproduce the problem
  • On the client computer that has Korean (CP949) as the default code page, start SQL Server Management Studio.
  • Connect to a database that has English (CP1252) as the default code page.
  • Create a table in the database by using the following line of code.
    Create table tbTest (A char(20), NA nchar(10), Comment char(20))
  • Insert a Korean character to the database by using the following query.
    Insert into tbTest (A,NA,Comment) values('가',N'가','SQL2005/INSERT')
  • Create a select query to retrieve the data by using the following line of code.
    select * from tbTest

You receive the following results. The value in column A is a question mark.

A                    NA         Comment
-------------------- ---------- --------------------
?                    가          SQL2005/INSERT
REFERENCES

For more information about the SQL Server collation and the Auto Translate parameter, click the following article numbers to view the articles in the Microsoft Knowledge Base:

162367 How to transfer Korean Double Byte Character Set chars

234748 You cannot correctly translate character data from a client to a server by using the SQL Server ODBC driver if the client code page differs from the server code page

For more information about SQL Server Unicode data types, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/aa902669(SQL.80).aspx


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • pulltorefresh插件可以輕鬆實現上拉下拉刷新,github.com上直接搜索進行下載。 佈局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ptr="http://sc
  • 這篇文章解釋了Java 虛擬機(JVM)的內部架構。下圖顯示了遵守 Java SE 7 規範的典型的 JVM 核心內部組件。 上圖顯示的組件分兩個章節解釋。第一章討論針對每個線程創建的組件,第二章節討論了線程無關組件。 線程 JVM 系統線程 每個線程相關的 程式計數器 棧 本地棧 棧限制 棧幀 局
  • 首先下載xUtils,下載地址:https://github.com/wyouflf/xUtils 把下載好的文件壓縮,把裡面的jar包拷進項目中如圖所示: 這裡新建一個User類進行測試增刪改查 package com.example.xutilsdemo; public class User {
  • 最近公司項目升級重構(重寫),除了本來我所負責的模塊,最後臨危受命接了推送(遠程和本地)相關的模塊,順便把推送的相關知識複習了一遍。後期連續工作十幾天加上最後一天的通(瞎)宵(熬)達(一)旦(夜),也算是不辱使命。此文除了講解遠程推送相關的基本知識外,也會涉及一些推送相關的奇淫技巧。另外本文主要講解
  • RecyclerView是 Android 相容包V21中新推出的列表類,它的自定義化強的優點足以讓它能夠取代GridView和ListView,本文將結合SwipeRefreshLayout與RecyclerView講解如何實現下拉刷新和自動載入的代碼 需要的依賴 以下版本自行更新 Java 1
  • 作為一名iOS開發者,我經歷過幾個沒有設計師的項目,結果就是,痛苦的一逼。做這種類型的項目,設計是非常重要的,特別是迭代設計。 在每個項目最開始的時候,客戶其實並不知道自己想要什麼。直接堆碼之前我們還是有點小小的設計知識更有助於你跟客戶撕逼的時候占上風,其實我們只是想更完美,難道不是嗎? 這裡我更推
  • 什麼是SQLite SQLite是一款輕型的嵌入式資料庫 它占用資源非常的低,在嵌入式設備中,可能只需要幾百K的記憶體就夠了 它的處理速度比Mysql、PostgreSQL這兩款著名的資料庫都還快 什麼是資料庫 資料庫(Database)是按照數據結構來組織、存儲和管理數據的倉庫 資料庫可以分為2大種
  • 當我看到這周的每周一文的題目是“關於對資料庫的認識和理解”的時候,我突然覺得無從下手,因為我不知道寫什麼,自己對資料庫的理解和接觸過的資料庫,僅僅限於課堂上老師講過的微軟公司的SQLServer2008,對於其他的資料庫自己也只是道聽途說。於是乎我瘋狂的在網上找資料,從不同的方面瞭解了當前的幾種主流
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...