Can anyone tell me how to set
the primary key on a datatable as all I can find in the help is how to
create a primary key on an XML file.
Private Sub GetPrimaryKeys(myTable As DataTable)
' Create the array for the columns.
Dim colArr() As DataColumn
colArr = myTable.PrimaryKey
' Get the number of elements in the array.
Console.WriteLine("Column Count: " & colArr.Length.ToString())
Dim i As Integer
For i = 0 To colArr.GetUpperBound(0)
Console.WriteLine(colArr(i).ColumnName &
colArr(i).DataType.ToString())
Next i
End Sub
Private Sub SetPrimaryKeys()
' Create a new DataTable and set two DataColumn objects as primary keys.
Dim myTable As DataTable = new DataTable()
Dim keys(2) As DataColumn
Dim myColumn As DataColumn
' Create column 1.
myColumn = New DataColumn()
myColumn.DataType = System.Type.GetType("System.String")
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
End Sub