none
如何把UnicodeField欄位轉成big5到Big5Field欄位, 然後再一次把Big5Field欄位轉成unicode到UnicodeField欄位 ?? RRS feed

  • 問題

  • 站長你好,我是Seven,我有一個問題想跟你發問,
    請可以幫幫我ㄇ??
    就是我想把UnicodeField欄位轉成big5到Big5Field欄位,
    然後再一次把Big5Field欄位轉成unicode到UnicodeField欄位
    但就是這兩個函數不知道哪裡出問題了(PS:這2個函數可以運作,但轉入轉出後資料怪怪的)

    my Database :
    +------------------+---------------+
    | UnicodeField | nvarchar(50) |
    +------------------+---------------+
    | Big5Field | varchar(50) |
    +------------------+---------------+
    | ChUnicodeField | nvarchar(50)|
    +------------------+---------------+


    // This button want to convert the unicode field to the big5 field,
    // and the converted data i want to put into the Big5Field field.
    protected void Button1_Click(object sender, EventArgs e)
    {
    SqlDataSource ads = new System.Web.UI.WebControls.SqlDataSource();
    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["EncodeConnectionString"];
    String conn = settings.ConnectionString;
    ads.ConnectionString = conn;

    ads.SelectCommand = "SELECT * FROM [ConvertTable]";
    DataView dv1 = (DataView)ads.Select(DataSourceSelectArguments.Empty);

    String UnicodeString;
    byte[] bBIG5;
    for (int i=0; i < dv1.Count; i++)
    {
    UnicodeString = Convert.ToString(dv1[i]["UnicodeField"]);
    bBIG5 = Encoding.GetEncoding("big5").GetBytes(UnicodeString);
    ads.UpdateCommand = " UPDATE [ConvertTable] SET [Big5Field] = '" + bBIG5 + "' WHERE [UnicodeField] = N'" + UnicodeString + "' ";
    }
    ads.Update();
    }

    // This button want to convert the big5 field to the unicode
    // field again,and the converted data i want to put into the
    // ChUnicodeField field.
    protected void Button2_Click(object sender, EventArgs e)
    {
    SqlDataSource ads = new System.Web.UI.WebControls.SqlDataSource();
    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["EncodeConnectionString"];
    String conn = settings.ConnectionString;
    ads.ConnectionString = conn;

    ads.SelectCommand = "SELECT * FROM [ConvertTable]";
    DataView dv1 = (DataView)ads.Select(DataSourceSelectArguments.Empty);

    byte[] bBIG5;
    byte[] utf8Bytes;
    UnicodeEncoding uniEncoding = new UnicodeEncoding();
    for (int i = 0; i < dv1.Count; i++)
    {
    // Get the data to write to the stream.
    bBIG5 = uniEncoding.GetBytes(Convert.ToString(dv1[i]["Big5Field"]));
    utf8Bytes = Encoding.Convert(Encoding.GetEncoding("BIG5"), Encoding.UTF8, bBIG5);
    UTF8Encoding encUtf8 = new UTF8Encoding();
    string utf8Str = encUtf8.GetString(utf8Bytes);
    ads.UpdateCommand = " UPDATE [ConvertTable] SET [ChUnicodeField] = '" + utf8Str + "' WHERE [Big5Field] = '" + bBIG5 + "' ";
    }
    ads.Update();
    }

    From Seven

    2010年2月6日 上午 06:50

解答

  • 看起來是迴圈有問題

    for (int i = 0; i < dv1.Count; i++) {
        UnicodeString = Convert.ToString(dv1[i]["UnicodeField"]);
        bBIG5 = Encoding.GetEncoding("big5").GetBytes(UnicodeString);
        ads.UpdateCommand +=  " UPDATE [ConvertTable] SET [Big5Field] = '" + bBIG5 + "' WHERE [UnicodeField] = N'" + UnicodeString + "' ";
    }
    ads.Update();




    for (int i = 0; i < dv1.Count; i++) {
        // Get the data to write to the stream.
        bBIG5 = uniEncoding.GetBytes(Convert.ToString(dv1[i]["Big5Field"]));
        utf8Bytes = Encoding.Convert(Encoding.GetEncoding("BIG5"), Encoding.UTF8, bBIG5);
        UTF8Encoding encUtf8 = new UTF8Encoding();
        string utf8Str = encUtf8.GetString(utf8Bytes);
        ads.UpdateCommand += " UPDATE [ConvertTable] SET [ChUnicodeField] = '" + utf8Str + "' WHERE [Big5Field] = '" + bBIG5 + "' ";
    }
    ads.Update();

    • 已編輯 阿尼Moderator 2010年2月6日 上午 07:07 補說明
    • 已標示為解答 sevenson_w 2010年2月6日 上午 10:57
    2010年2月6日 上午 07:05
    版主

所有回覆

  • 看起來是迴圈有問題

    for (int i = 0; i < dv1.Count; i++) {
        UnicodeString = Convert.ToString(dv1[i]["UnicodeField"]);
        bBIG5 = Encoding.GetEncoding("big5").GetBytes(UnicodeString);
        ads.UpdateCommand +=  " UPDATE [ConvertTable] SET [Big5Field] = '" + bBIG5 + "' WHERE [UnicodeField] = N'" + UnicodeString + "' ";
    }
    ads.Update();




    for (int i = 0; i < dv1.Count; i++) {
        // Get the data to write to the stream.
        bBIG5 = uniEncoding.GetBytes(Convert.ToString(dv1[i]["Big5Field"]));
        utf8Bytes = Encoding.Convert(Encoding.GetEncoding("BIG5"), Encoding.UTF8, bBIG5);
        UTF8Encoding encUtf8 = new UTF8Encoding();
        string utf8Str = encUtf8.GetString(utf8Bytes);
        ads.UpdateCommand += " UPDATE [ConvertTable] SET [ChUnicodeField] = '" + utf8Str + "' WHERE [Big5Field] = '" + bBIG5 + "' ";
    }
    ads.Update();

    • 已編輯 阿尼Moderator 2010年2月6日 上午 07:07 補說明
    • 已標示為解答 sevenson_w 2010年2月6日 上午 10:57
    2010年2月6日 上午 07:05
    版主
  • 謝謝你的回信,

    我現在來試試看這程式,寫入資料庫的運作狀況.

    感恩!!

    2010年2月6日 上午 10:59