Code
private void GetPrimaryKeys(DataTable myTable)
{
// Create the array for the columns.
DataColumn[] colArr;
colArr = myTable.PrimaryKey;
// Get the number of elements in the array.
Console.WriteLine("Column Count: " + colArr.Length);
for(int i = 0; i < colArr.Length; i++)
{
Console.WriteLine(colArr[i].ColumnName + colArr[i].DataType);
}
}
private void SetPrimaryKeys()
{
// Create a new DataTable and set two DataColumn objects as primary keys.
DataTable myTable = new DataTable();
DataColumn[] keys = new DataColumn[2];
DataColumn myColumn;
// Create column 1.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
//System.Decimal boolean、string、float Int32
myColumn.ColumnName= "FirstName";
// Add the column to the DataTable.Columns collection.
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[0] = myColumn;
// Create column 2 and add it to the array.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
myColumn.ColumnName = "LastName";
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[1] = myColumn;
// Set the PrimaryKeys property to the array.
myTable.PrimaryKey = keys;
}
private void GetPrimaryKeys(DataTable myTable)
{
// Create the array for the columns.
DataColumn[] colArr;
colArr = myTable.PrimaryKey;
// Get the number of elements in the array.
Console.WriteLine("Column Count: " + colArr.Length);
for(int i = 0; i < colArr.Length; i++)
{
Console.WriteLine(colArr[i].ColumnName + colArr[i].DataType);
}
}
private void SetPrimaryKeys()
{
// Create a new DataTable and set two DataColumn objects as primary keys.
DataTable myTable = new DataTable();
DataColumn[] keys = new DataColumn[2];
DataColumn myColumn;
// Create column 1.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
//System.Decimal boolean、string、float Int32
myColumn.ColumnName= "FirstName";
// Add the column to the DataTable.Columns collection.
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[0] = myColumn;
// Create column 2 and add it to the array.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
myColumn.ColumnName = "LastName";
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[1] = myColumn;
// Set the PrimaryKeys property to the array.
myTable.PrimaryKey = keys;
}