The data stored in the database is in unicode format. Can it be read in. NET and converted to traditional Chinese characters?

Source: Internet
Author: User

1. The data format stored in the database is unicode, and N people have not solved the problem.

2. to read data in. NET and convert it to traditional Chinese, follow these steps:

Unicode string-> unicode byte-> gb2312 byte-> big5 string

3. The sample image is as follows:

4. The solution is as follows:

 

Private void Form1_Load (object sender, EventArgs e)
{
ReadDataFromDatabase ();
}

Private void ReadDataFromDatabase ()
{
SqlConnection oConnection;
// Modify Database Connection Parameters
OConnection = GetDBConnection (".", "testdb", "sa", "123 ");

If (oConnection = null)
{
MessageBox. Show ("Can't connect to DB! ");
Return;
} // If (oConnection = null)

SqlCommand oCommand = new SqlCommand ();
OCommand. CommandText = "select top 100 [InfoID], [CateID], [Content], [Title] FROM [Info]";
OCommand. Connection = oConnection;
OCommand. CommandType = CommandType. Text;
SqlDataAdapter oDataAdapter = new SqlDataAdapter (oCommand );
DataSet oDataSet = new DataSet ();
ODataAdapter. Fill (oDataSet, "Info ");

OConnection. Close ();

If (oDataSet. Tables. Count <1)
{
MessageBox. Show ("No datatable! ");
Return;
} // If (oDataSet. Tables. Count <1)

DataTable oTable = oDataSet. Tables [0];

DataRow oRow;

If (oTable. Rows. Count <1)
{
MessageBox. Show ("No data rows! ");
Return;
} // If (oDataSet. Rows. Count <1)

StringBuilder oStringBuilder = new StringBuilder ();
For (int I = 0; I <oTable. Rows. Count; I ++)
{
ORow = oTable. Rows [I];
OStringBuilder. append (string. format ("{0}, {1}, {2} \ r \ n", oRow [0]. toString (), oRow [1]. toString (), ConvertCodePage (oRow [2]. toString ())));
}

TextBox1.Text = oStringBuilder. ToString ();

}

Private string ConvertCodePage (string stringData)
{

Encoding oUnicodeEncoding = Encoding. Unicode;
Encoding oGB2312Encoding = System. Text. Encoding. GetEncoding ("gb2312 ");

// Convert the string into a byte [].
Byte [] oldBytes = oUnicodeEncoding. GetBytes (stringData );

Byte [] newBytes = Encoding. Convert (oUnicodeEncoding, oGB2312Encoding, oldBytes );

Encoding oBig5Encoding = System. Text. Encoding. GetEncoding ("big5 ");

Char [] newChars = new char [oGB2312Encoding. GetCharCount (newBytes, 0, newBytes. Length)];
OBig5Encoding. GetChars (newBytes, 0, newBytes. Length, newChars, 0 );

 

Return (new string (newChars ));
}

 

Private SqlConnection GetDBConnection (string dbServer, string dbName, string dbUser, string dbPassword)
{
SqlConnection oConnection;
String strConnString = string. format ("Server = {0}; Database = {1}; User ID = {2}; Password = {3}; Trusted_Connection = False;", dbServer, dbName, dbUser, dbPassword );

Try
{
OConnection = new SqlConnection ();
OConnection. ConnectionString = strConnString;
OConnection. Open ();
Return oConnection;
}
Catch
{
Return null;
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.