本文整理汇总了C#中System.Data.DataTable.PrimaryKey属性的典型用法代码示例。如果您正苦于以下问题:C# DataTable.PrimaryKey属性的具体用法?C# DataTable.PrimaryKey怎么用?C# DataTable.PrimaryKey使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Data.DataTable
的用法示例。
在下文中一共展示了DataTable.PrimaryKey属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPrimaryKeys
private void GetPrimaryKeys(DataTable table)
{
// Create the array for the columns.
DataColumn[] columns;
columns = table.PrimaryKey;
// Get the number of elements in the array.
Console.WriteLine($"Column Count: {columns.Length}");
for (int i = 0; i < columns.Length; i++)
{
Console.WriteLine($"{columns[i].ColumnName} {columns[i].DataType}");
}
}
private void SetPrimaryKeys()
{
// Create a new DataTable and set two DataColumn objects as primary keys.
var table = new DataTable();
var keys = new DataColumn[2];
DataColumn column;
// Create column 1.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName= "FirstName";
// Add the column to the DataTable.Columns collection.
table.Columns.Add(column);
// Add the column to the array.
keys[0] = column;
// Create column 2 and add it to the array.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "LastName";
table.Columns.Add(column);
// Add the column to the array.
keys[1] = column;
// Set the PrimaryKeys property to the array.
table.PrimaryKey = keys;
}