确切的异常是 System.Data.dll 中发生的“System.Data.MissingPrimaryKeyException”
附加信息:表没有主键。
但是,我已经设置了主键。这是代码。谢谢。
DataColumn[] PrimaryKeyColumns; //Global
DataTable firstLinesDT = new DataTable();
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "FirstLines";
column.Unique = true;
firstLinesDT.Columns.Add(column);
PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = column;
firstLinesDT.PrimaryKey = PrimaryKeyColumns;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "Length";
firstLinesDT.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "FilePath";
firstLinesDT.Columns.Add(column);
**// EXCEPTION THROWN AT FIND OPERATION**
DataRow foundRow = firstLinesDT.NewRow();
foundRow = firstLinesDT.Rows.Find(line);
根据 msdn,这正是应该设置主键的方式 - http://msdn.microsoft.com/en-us/library/system.data.datatable.primarykey.aspx