在C#.Net中,为DataTable设置了主键,但出现异常“Table没有主键”被抛出
确切的例外是 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
The precise exception is
'System.Data.MissingPrimaryKeyException' occurred in System.Data.dll
Additional information: Table doesn't have a primary key.
However, I have set the primary key. This is the code. Thanks.
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);
And according to msdn, this exactly how the primary key is supposed to be set-
http://msdn.microsoft.com/en-us/library/system.data.datatable.primarykey.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,PrimaryKey.Length 为 0,因为我在用适配器中的行填充
DataTable
之前声明了主键数组。先尝试用数据填充它I had the same problem and PrimaryKey.Length was 0 because I was declaring the Primary keys array before I filled the
DataTable
with rows from the adapter. Try to fill it with data first我不是 100% 确定,但我相信在设置所有其他列之前您不会添加主键。
换句话说,将
firstLinesDT.PrimaryKey = PrimaryKeyColumns;
移动到添加FilePath
列的位置之后。I'm not 100% certain, but I believe you don't add the primary key until all of the other columns are set.
In other words move
firstLinesDT.PrimaryKey = PrimaryKeyColumns;
to be after the point where you add theFilePath
column.我不知道这是否回答了您的问题,因为您的代码不包含
DataView
,您可能将其遗漏,因为您认为它在示例中并不重要。但是,将表转换为 DataView,然后返回
ToTable()
会删除 PrimaryKey。I don't know if this answers your question as your code does not include a
DataView
, it's possible that you left it out as you didn't think it was important in the example.However, turning a table into a DataView, and then back
ToTable()
, removes the PrimaryKey.