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;
}
}