数据库存储的数据为 unicode格式,在.NET 读取出来并转换为繁体字?

1.数据库存储的数据格式是unicode ,咨询N多人也没有解决问题。

2.要在.NET正常读取,并转换为繁体字,是经过下列步骤:

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

3.样图如下:

4.解决方案如下:

 

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

        private void ReadDataFromDatabase()
        {
            SqlConnection oConnection;
            //修改 数据库连接 参数
            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;
            }


        }

原文地址:https://www.cnblogs.com/ggbbeyou/p/1662903.html