Primary Key

R

Richard

Hi

I'm trying to create a relationship between two datatables in a dataset but
I think I need to set the primary key first. 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.

Thanks
Richard
 
S

scorpion53061

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
 
C

Cor Ligthert

Hi Richard,

When you make the dataset yourself, have a look at the method Scorpion
provided, however when you get them from a fill you can also add this before
the Fill.

da.MissingSchemaAction = MissingSchemaAction.AddWithKey

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top