我正在使用VSTS 2008 + C#+ .Net 3.0。我有兩個輸入字符串,我認爲它們是不同的。但是下面的C#代碼認爲它們是相同的,並拋出System.Data.ConstraintException,表示列名不是唯一的,但值已經存在。任何想法有什麼不對?C#字符串重複問題
這裏是我的代碼和我的輸入字符串,我輸入的字符串
十六進制查看,我的輸入字符串的
http://i30.tinypic.com/2anx2b.jpg
記事本查看,
http://i30.tinypic.com/2q03hn4.jpg
我代碼,
static void Main(string[] args)
{
string[] buf = new string[] { "2ch", "2ch" };
DataTable bulkInserTable = new DataTable("BulkTable");
DataColumn column = null;
DataRow row = null;
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Name";
column.ReadOnly = true;
column.Unique = true;
bulkInserTable.Columns.Add(column);
foreach (string item in buf)
{
row = bulkInserTable.NewRow();
row["Name"] = item;
bulkInserTable.Rows.Add(row);
}
}
編輯1:
我的困惑是,爲什麼C#字典認爲它們是不同的,但數據集認爲它們是相同的。任何解決方案使行爲一致?這裏是我的代碼來證明C#Dictionary認爲它們不同,返回buf數組有兩個元素。
Dictionary<string, bool> dic = new Dictionary<string, bool>();
foreach (string s in buf)
{
dic[s] = true;
}
buf = new List<string>(dic.Keys).ToArray(); // we got two strings here, other than one, which proves Dictionary thinks the two strings are different.
拉丁字符的「正常」和完整/半角形式通常被認爲是等價的,如果你正在處理文本。 – Joey 2009-08-05 14:23:17
感謝Johannes,我想了解更多關於什麼意思是完整和半角形式的拉丁文,你能推薦一些讀物嗎? – George2 2009-08-05 14:34:49