设置主键意味着列值必须唯一。
DataTable似乎并没有将密钥强制为 ,而是使用DataTable.Locale比较规则将它们比较为非零。
即使字符串不同(排序顺序相同),DataTable.Locale.CompareInfo.Compare也可能返回0。
有没有办法改变行为?我找不到如何替换CompareInfo。
我拥有的示例数据包括 UnicodeCategory.OtherNotAssigned 类别中的一些字符。
using System;
using System.Data;
namespace datatabletest
{
class Program
{
static void Main( string[] args )
{
string key = "key";
string metric = "metric";
string key1 = "\u0a0dtest\u0a0d;";
string key2 = "test\u0a0d;";
DataTable table = new DataTable("table");
table.Columns.Add( key);
table.Columns.Add( metric );
table.PrimaryKey = new[] { table.Columns[key] };
table.BeginLoadData();
{
DataRow row = table.NewRow();
row[key] = key1;
row[metric] = "500";
table.Rows.Add( row );
}
{
DataRow row = table.NewRow();
row[key] = key2;
row[metric] = "500";
table.Rows.Add( row );
}
// strings are different
Console.WriteLine( key1 == key2 );
//but they compare method still returns 0
Console.WriteLine( table.Locale.CompareInfo.Compare( key1, key2 ) );
// throws a ConstraintException
table.EndLoadData();
}
}
}