Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Lookup where is the primary key of a foreign key
 

Lookup where is the primary key of a foreign key

Hi all,

I want to know how, given the name of a field in a table,

verify if it is a foreign key, and obtain the table where it is primary,

the idea behind is to obtain automatically the distinct values automatically in a search module.

'return a list of all fields in a table

Public Function myFields(ByRef table As DataTable) As ArrayList

Dim arr As New ArrayList

For i As Integer = 0 To table.Columns.Count - 1

myFieldsPrimary(table.TableName)

arr.Add(table.Columns(i).ColumnName)

Next

Return arr

End Function

Private Function myFieldsPrimary(ByVal FieldName As String) As ArrayList

'Return

'so basically i get fields of a table from the function myfields,. these are fed in a combobox, and when the user selects one of the field in the combobox, this method should return the values that this field's primary table contains, returns an arraylist to store in my combo2.

End Function

hrubesh  Tuesday, August 29, 2006 10:20 AM

If the table froma sql server database you can use the smo class to get that info.

The smo class is new to framework 2.0

Add a reference to microsoft.sqlserver.smo and microsoft.sqlserver.connectioninfo

Imports Microsoft.SqlServer.Management.Smo

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim srv As New Server(".")
Dim db As Database = srv.Databases("NorthWind")
Dim tbl As Table = db.Tables("Products")

For Each fk As ForeignKey In tbl.ForeignKeys
Debug.Print(fk.Name & " is from Table " & fk.Parent.Name)
Next
End Sub
End Class

Ken Tucker  Tuesday, August 29, 2006 11:02 AM

If the table froma sql server database you can use the smo class to get that info.

The smo class is new to framework 2.0

Add a reference to microsoft.sqlserver.smo and microsoft.sqlserver.connectioninfo

Imports Microsoft.SqlServer.Management.Smo

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim srv As New Server(".")
Dim db As Database = srv.Databases("NorthWind")
Dim tbl As Table = db.Tables("Products")

For Each fk As ForeignKey In tbl.ForeignKeys
Debug.Print(fk.Name & " is from Table " & fk.Parent.Name)
Next
End Sub
End Class

Ken Tucker  Tuesday, August 29, 2006 11:02 AM

shot bra , just finished working on that , i've done it like that:

with one problem, my pk ref is 0 always. i'll chek on that now, there can be more than 1 pk and i do want a general solution.

'given the dataset and the table, it returns an arraylist of combos already binded.

'the combos are created according to the parent relationship of the base table.

'that is. example table bill has fk suppliercode from supplier, it opens supplier

'and binds suppliercode to a combo. the combo is added to a list.

Public Function myForeignValues(ByRef ds As DataSet, ByVal tb As String) As ArrayList

Dim arr As New ArrayList

Dim s As ComboBox = Nothing

For i As Int16 = 0 To ds.Tables(tb).ParentRelations.Count - 1

s = New ComboBox

s.Dock = DockStyle.Top

s.DataSource = ds.Tables(tb).ParentRelations(i).ParentTable

s.ValueMember = ds.Tables(tb).ParentRelations(i).ParentTable.PrimaryKey(0).ToString

s.DisplayMember = ds.Tables(tb).ParentRelations(i).ParentTable.PrimaryKey(0).ToString()

arr.Add(s)

Next

Return arr

End Function

hrubesh  Tuesday, August 29, 2006 11:30 AM

You can use google to search for other answers

Custom Search

More Threads

• Database Connection
• SQLDataSource - setting parameter values in code
• Question about setting combobox contents in a gridview
• Auto Format DataGridView
• Change cell style of specific column in DataGridView
• Validate Cell Value in Datagridview
• serial no problem in datagrid..
• Problem with DataGridView
• Problem with calling web-service (WCF) through WinForms...
• VB.NET DataGrid