设置DataTable的主键

11
objDataTable.Columns.Add("Student Id", typeof(int));
objDataTable.PrimaryKey = "Student Id";

我已经尝试过很多方法,但是我做不到。

在C#中如何将主键设置为这一行?朋友们请帮忙。

先行谢过。

1个回答

32

试试这个:

DataTable objDataTable = new DataTable();
objDataTable.Columns.Add("Student Id", typeof(int));
objDataTable.PrimaryKey = new DataColumn[] { objDataTable.Columns["Student Id"] };
该属性PrimaryKey的类型为Type: System.Data.DataColumn[]而非字符串。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接