【发布时间】:2011-12-06 15:21:55
【问题描述】:
我正在尝试在 DataTable 中搜索我知道存在的行。
// This is the row my search should find
DataRow goal = dtLkupCat.Rows[6];
// This finds the row correctly
string srchexpr = String.Format("sport = '{0}' and catcode = '{1}' and type = '{2}' and [parent] = '{3}' and code = '{4}'", goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"]);
DataRow[] test = dtLkupCat.Select(srchexpr);
// But if I set a PK and search for the values I know to be correct, it returns null
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
DataRow lkup = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"] });
它正在搜索的列/值没有什么特别之处。它们都是有效的字符串,没有一个是 null/DBNull。我在这里想念什么?显然,我可以使用 Select() 作为解决方法,但想知道 Find() 为什么不起作用。
更新:如果有人想尝试一下,我已经从我的查找表的一个子集中发布了 xml。下载地址:http://www.flantech.net/files/lkup_cat2.zip
然后尝试运行此代码。奇怪的是,它会使用四列的不同组合来查找行,但永远不会使用所有五列。
DataTable dtLkupCat = new DataTable("lkup_cat");
dtLkupCat.ReadXml(@"lkup_cat2.xml");
// This is the row my search should find
DataRow goal = dtLkupCat.Rows[0];
// This is how I need to do the search, but it doesn't find the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
DataRow found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "sport" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["catcode"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "catcode" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "type" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
【问题讨论】:
-
我不知道答案,但我正在做一些猜测。一种可能性是列定义不明确。也许您也可以致电
DataSet.FillSchema。另一种可能性可能是有 2 行具有相同的主键集。尝试检查test.Length。还要检查您的任何dtLkupCat.Columns["xyz"]是否为null。 -
test.Length 为1,如果key列有重复或空值,设置PK时会报错。