LoadDataRow方法的问题
程序代码:
DataTable table = new DataTable(); DataColumn col1 = new DataColumn("id ", typeof(string)); DataColumn col2 = new DataColumn("name ", typeof(string)); DataColumn col3 = new DataColumn("age ", typeof(int)); DataColumn[] key = new DataColumn[1]; table.Columns.Add(col1); table.Columns.Add(col2); table.Columns.Add(col3); key[0] = col1; table.PrimaryKey = key; DataRow row = table.NewRow(); row[col1] = "G001 "; row[col2] = "yanxiangliang "; row[col3] = "31 "; table.Rows.Add(row); row = table.NewRow(); row[col1] = "G002 "; row[col2] = "zhangzhiguo "; row[col3] = "32 "; table.Rows.Add(row); row = table.NewRow(); row[col1] = "G003 "; row[col2] = "ls "; row[col3] = "30 "; table.Rows.Add(row); Object[] find = new Object[3]; find[0] = "G001"; find[1] = "teswt "; find[2] ="45"; table.BeginLoadData(); table.LoadDataRow(find, true); table.EndLoadData(); Console.WriteLine(table.Rows[0][1].ToString()); Console.ReadLine();最近在重温ADO时看到datatable的LoadDataRow方法有个疑问,LoadDataRow是按照主键查找的,按照msdn的意思是找到一样行的话可以更新行,否则就可以新增行。但是我把要查找行的主键(红色标注为要查找行)设置为与某行主键一样,本意是想查找相同行后更新其他列,但是程序运行到最后报主键冲突错误,这让我很奇怪既然报了主键冲突那么LoadDataRow又是如何查找行的呢?难不成LoadDataRow只能用来增加新行?如果是这样那这个方法也太鸡肋了吧。