This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20385|Reply: 0

[Source] Translate all the field names of the DataTable into Chinese and return

[Copy link]
Posted on 10/29/2015 1:50:20 PM | | | |
Original data:

Translated data:



Generally, it is easy to query and display a DataTable in the code, as long as the queried DataTable is used as the DataSource of a certain control, it is OK, but the fields of this DataTable are all English fields, how to change them all to Chinese?



Now I know the Chinese names of each English field, and all the translation information of the fields is stored in a table, called COLUMNTRANSLATION, and the SQL statement to create this table is:

create table COLUMNTRANSLATION

(
    ID   NUMBER(10) PRIMARY KEY,
    COLUMNNAME VARCHAR2(50), //field name
    TRANSLATION VARCHAR2(50) // field corresponding to the Chinese name
)



Put a few data in it first, so that there are several records in this table, for example: StudentName corresponds to "student name".



Now that I have a DataTable with all its field names in English, I can translate it and return the translated DataTable by simply executing the following code:

  1. /// <summary>
  2.         /// 将DataTable的字段名全部翻译为中文
  3.         /// </summary>
  4.         /// <param name="table">待翻译的DataTable</param>
  5.         /// <returns></returns>
  6.         public DataTable TranslateDataTable(DataTable table)
  7.         {
  8.             DataTable dt = new DataTable();
  9.             dt.TableName = "TempTable";

  10.             if (table != null && table.Rows.Count > 0)
  11.             {
  12.                 //先为dt构造列信息
  13.                 foreach (DataColumn column in table.Columns)
  14.                 {
  15.                     string name = GetColumnName(column.ColumnName);
  16.                     dt.Columns.Add(name);
  17.                 }

  18.                 for (int i = 0; i < table.Rows.Count; i++)
  19.                 {
  20.                     DataRow NewRow = dt.NewRow();
  21.                     DataRow row = table.Rows[i];

  22.                     for (int j = 0; j < dt.Columns.Count; j++)
  23.                     {
  24.                         NewRow[j] = row[j].ToString();
  25.                     }

  26.                     dt.Rows.Add(NewRow);
  27.                 }
  28.             }
  29.             return dt;
  30.         }

  31.         /// <summary>
  32.         /// 得到列名称的别名
  33.         /// </summary>
  34.         /// <param name="columnName"></param>
  35.         /// <returns></returns>
  36.         private string GetColumnName(string columnName)
  37.         {
  38.             string sqlString = " Select TRANSLATION from COLUMNTRANSLATION where COLUMNNAME = '" + columnName.ToUpper() + "'";

  39.             object s = dao.ExecuteScalar(sqlString);
  40.             string name = (s == null) ? columnName : s.ToString(); //如果此英文字段有翻译,则返回其中文翻译,若无,则保留此英文翻译。
  41.             return name;
  42.         }
Copy code
The idea of this method is to construct a new DataTable according to the DataTable to be translated: the column name of each column of the new DataTable is the translated Chinese name (if the English column name is not translated, the English column name is retained, if it can be translated, it is translated), and after constructing the field information of the new DataTable, the records of the old DataTable are inserted into the new DataTable one by one.



Hehe, it's a turn... But speed is still possible.







Previous:EasyUI datagrid tables dynamically bind field columns
Next:Turn off the Browser Link feature in Visual Studio 2013
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com