本文整理匯總了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;
}