次の方法で共有


Case sensitive or not sensitive

In a Dataset, case sensitive seems only apply to the data not to the schema

Within a dataset, table and column names are by default case-insensitive — that is, a table in a dataset called "Customers" can also be referred to as "customers." This matches the naming conventions in many databases, including the default behavior of SQL Server, where the names of data elements cannot be distinguished only by case.

Look at carefully for the following code:

VB ------------------------------------------

            Dim ds As DataSet =  New DataSet()
            Dim t1 As DataTable =  New DataTable("ABC")
            Dim t2 As DataTable =  New DataTable("abc")
            ds.Tables.Add(t1)
            Dim b1 As Boolean =  ds.Tables.Contains("Abc")
            ' return true
            Dim t3 As DataTable =  ds.Tables("Abc")
            ' return ABC
 
            ds.Tables.Add(t2)
            Dim b2 As Boolean =  ds.Tables.Contains("ABC")
            Dim b3 As Boolean =  ds.Tables.Contains("abc")
            Dim b4 As Boolean =  ds.Tables.Contains("Abc")
            Dim t4 As DataTable =  ds.Tables("ABC")
            Dim t5 As DataTable =  ds.Tables("abc")
            ' this would throw dim t6 as DataTable = ds.Tables["Abc"]

C#--------------------------------

DataSet ds = new DataSet();

DataTable t1 = new DataTable("ABC");

DataTable t2 = new DataTable("abc");

ds.Tables.Add(t1);

bool b1 = ds.Tables.Contains("Abc");

// return true, case insensitive

DataTable t3 = ds.Tables["Abc"];

// return ABC

ds.Tables.Add(t2);

bool b2 = ds.Tables.Contains("ABC"); // true

bool b3 = ds.Tables.Contains("abc"); // true

bool b4 = ds.Tables.Contains("Abc"); // false now -- Case sensitive now

DataTable t4 = ds.Tables["ABC"]; // ok

DataTable t5 = ds.Tables["abc"]; // ok

// this would throw DataTable t6 = ds.Tables["Abc"]

Karol and I have encountered this in our dataset designer and this is by design from DataSet team. So you need to be careful not be fooled. We alway treat the table name in a case sensitive way with our own version of Contains functions like:

public static bool ContainsTable( DataTableCollection tables, string name ) {

if( tables.Contains(name) ) {

DataTable table = tables[name];

if( StringUtil.EqualValue(table.TableName, name) ) {

return true;

}

}

return false;

}

This case sensitive issue applies to DataColumn, DataRelation and Constrains too.

JohnChen (Zhiyong)

Smart Client Data Team

Comments