首页 > 解决方案 > 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:

enter image description here

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:

enter image description here

It seems that the toLower() of the uppercase "I" is different in turkish then in other languages.

标签: c#datatable.net-2.0

解决方案


尝试使用土耳其区域设置在字符串“compID”上创建一个 toLower()。如果它将返回“compID”,则 türkisch 区分大小写不同于西欧。其实我也查不出来,因为我这里没有电脑。


推荐阅读