c# - DataTable Column CaseSensitive in Turkey
问题描述
I have a customer in Turkey who had a problem recently that he couldn't use one of our components. The component is written in C# .Net 2.0 and uses a DataTable
which has several columns. If I access a column by name on my computer(Region Germany and Language German) then the component works. If I execute the component on a turkish computer(Region Turkey and language Türkçe) it fails to execute properly.
I narrowed the problem down to case sensitivity. If I access a column by name and the name is written with an uppercase letter instead of lowercase letters then it fails on the turkish computer. The strange thing for me is, that it won't fail on my computer. Both machines are Windows 10.
I have created an example to make this problem reproducable.
class Program
{
static void Main(string[] args)
{
DataSet set = GetDataSet();
try
{
foreach (DataRow row in set.Tables[0].Rows)
{
String Directory = string.Format("{0}", row["component"]);
String FileName = row["name"].ToString();
String compBig = String.Format("{0}", row["compId"]); //here is the problem. I wrote the compId with an uppercase I instead of i
String component = String.Format("{0}", row["component"]);
String name = row["name"].ToString();
String compSmall = row["compid"].ToString();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
private static DataSet GetDataSet()
{
DataSet set = new DataSet();
DataTable workTable = new DataTable("Table0");
workTable.Columns.Add("component", typeof(int));
workTable.Columns.Add("name", typeof(String));
workTable.Columns.Add("compid", typeof(Guid)); //Here is the compid with a lowercase i
DataRow row = workTable.NewRow();
row["component"] = 0;
row["name"] = "AName";
row["compid"] = Guid.NewGuid();
workTable.Rows.Add(row);
set.Tables.Add(workTable);
return set;
}
}
The Exception is as follows:
My question is: Why is it in one situation case sensitive and in another one case insensitive?
I solved the problem for now, by changing the uppercase I to lowercase i (which it should have been from the get go)
--- UPDATE: ---
I have tried it now with several cultureInfos
CultureInfo culture;
//uncomment to test
culture = CultureInfo.CreateSpecificCulture("tr-TR"); //turkish fails
//culture = CultureInfo.CreateSpecificCulture("he-IL"); //Hebrew works
//culture = CultureInfo.CreateSpecificCulture("ar-SA"); //Arabic works
//culture = CultureInfo.CreateSpecificCulture("en-GB"); //English works
//culture = CultureInfo.CreateSpecificCulture("ru-RU"); //Russian works
//culture = CultureInfo.CreateSpecificCulture("de-DE"); //German works
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
until now turkish seems to be the only one where it fails.
--- Update ---
I tried the following:
culture = CultureInfo.CreateSpecificCulture("tr-TR"); //turkish fails
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
String ID = "ID";
String idLower = "id";
Console.WriteLine(ID.ToLower());
Console.WriteLine(idLower.ToUpper());
culture = CultureInfo.CreateSpecificCulture("el-GR"); //Greek works
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
Console.WriteLine(ID.ToLower());
Console.WriteLine(idLower.ToUpper());
which results in:
It seems that the toLower() of the uppercase "I" is different in turkish then in other languages.
解决方案
尝试使用土耳其区域设置在字符串“compID”上创建一个 toLower()。如果它将返回“compID”,则 türkisch 区分大小写不同于西欧。其实我也查不出来,因为我这里没有电脑。
推荐阅读
- css - 内容不会移动到指定的网格区域
- python - 我应该如何使用 ThreadPoolExecutor 管理实例方法的并行优化?
- git - Gitlab runner - 致命:找不到存储库“http://gitlab.dev.pl/user/repo-name.git/”
- ios - 我想在单击按钮时在另一个视图(包含按钮)旁边显示一个视图。macOS 的 SwiftUI 可以吗?
- javascript - 似乎无法使用 cypress 在下拉列表中选择一个选项
- html - php xpath 查询:如何在具有标题属性的 HTML 文档中查找所有内容?
- r - R Shiny - 在 Reactive 中使用 eventReactive 输出
- swift - 为什么 Swift 解释器为抛出运行时错误的实例提供内存位置,而不是类名?
- javascript - 如何在javascript中获取时区偏移量?
- sql - Oracle 每个值创建 N 行