在C#.Net中,为DataTable设置了主键,但出现异常“Table没有主键”被抛出

发布于 2024-12-17 10:49:01 字数 1162 浏览 0 评论 0原文

确切的例外是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹梦到西洲 2024-12-24 10:49:01

我遇到了同样的问题,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

甲如呢乙后呢 2024-12-24 10:49:01

我不是 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 the FilePath column.

笨笨の傻瓜 2024-12-24 10:49:01

我不知道这是否回答了您的问题,因为您的代码不包含 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文