amw22
asked on
Creating relationships in VB.Net using ADO.Net
I am creating an VB.Net program that is using a MS Access database on the backend. I need to create a relationship between two tables but I am not familar with how to do this with ADO.Net.
Right now I have the following data adapters and datasets:
Dim da1 As New OleDb.OleDbDataAdapter("SE LECT * FROM table1", Conn)
Dim da2 As New OleDb.OleDbDataAdapter("Se lect * From table2", Conn)
da1.Fill(ds_XWALK, "table1")
da2.Fill(ds_mpsms, "table2")
How do I make a relationship between two tables (and two datasets) using ADO.Net in code (I do not want to use any of the wizards for this, I would like to code it all myself) ?
Right now I have the following data adapters and datasets:
Dim da1 As New OleDb.OleDbDataAdapter("SE
Dim da2 As New OleDb.OleDbDataAdapter("Se
da1.Fill(ds_XWALK, "table1")
da2.Fill(ds_mpsms, "table2")
How do I make a relationship between two tables (and two datasets) using ADO.Net in code (I do not want to use any of the wizards for this, I would like to code it all myself) ?
Oh and then after you do that you have to add your relation to the dataset, as in:
ds_XWALK.Relations.Add(myR elation)
ds_mpsms.Relations.Add(myR elation2) etc.
ds_XWALK.Relations.Add(myR
ds_mpsms.Relations.Add(myR
Oh and if you want to add a constraint to a column:
Dim myConstraint As New UniqueConstraint(myDataCol umn)
ds_XWALK.Constraints.Add(m yConstrain t)
Dim myConstraint As New UniqueConstraint(myDataCol
ds_XWALK.Constraints.Add(m
ASKER
What does "Data Relation 1" specify in the line below?
Dim myRelation As New DataRelation("Data Relation 1", Column1, Column2)
Dim myRelation As New DataRelation("Data Relation 1", Column1, Column2)
I belive its just an identifier
Hee is a good example that you should be able to adapt to your project:
Private Sub CreateRelation()
' Get the DataColumn objects from two DataTable objects in a DataSet.
Dim parentCol As DataColumn
Dim childCol As DataColumn
' Code to get the DataSet not shown here.
parentCol = DataSet1.Tables("Customers ").Columns ("CustID")
childCol = DataSet1.Tables("Orders"). Columns("C ustID")
' Create DataRelation.
Dim relCustOrder As DataRelation
relCustOrder = New DataRelation("CustomersOrd ers", parentCol, childCol)
' Add the relation to the DataSet.
DataSet1.Relations.Add(rel CustOrder)
End Sub
Private Sub CreateRelation()
' Get the DataColumn objects from two DataTable objects in a DataSet.
Dim parentCol As DataColumn
Dim childCol As DataColumn
' Code to get the DataSet not shown here.
parentCol = DataSet1.Tables("Customers
childCol = DataSet1.Tables("Orders").
' Create DataRelation.
Dim relCustOrder As DataRelation
relCustOrder = New DataRelation("CustomersOrd
' Add the relation to the DataSet.
DataSet1.Relations.Add(rel
End Sub
ASKER
The example above uses only one dataset. In my project I have two different datasets. Is it possible to create relationships between tables in two different datasets, and what is the circumstances in which I would want one dataset over multiple datasets?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oh, And of course, you have to dimesion your parentCol and childCol variables:
Dim parentCol As DataColumn
Dim childCol As DataColumn
Dim parentCol As DataColumn
Dim childCol As DataColumn
ASKER
I tried creating the relation using the two different datasets as you explained above, but it gives an error saying it cannot create a relationship between tables in two different datasets.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The two tables should have a one to many relationship.
This is what I have tried and it gives me an error saying a relationship cannot be made between tables in different datasets:
Dim da_XWALK As New OleDb.OleDbDataAdapter("SE LECT * FROM XWALKV43II", Conn)
Dim da_mpsms As New OleDb.OleDbDataAdapter("Se lect * From MPSMS", Conn)
da_XWALK.Fill(ds_XWALK, "XWALKV43II")
da_mpsms.Fill(ds_mpsms, "MPSMS")
ds_XWALK.EnforceConstraint s = False
ds_mpsms.EnforceConstraint s = False
Dim parentCol As DataColumn = ds_XWALK.Tables("XWALKV43I I").Column s("MPSMS1" )
Dim childCol As DataColumn = ds_mpsms.Tables("MPSMS").C olumns("MP SMSCode")
Dim relMPSMS As DataRelation
relMPSMS = New DataRelation("MPSMS", parentCol, childCol)
ds_XWALK.Relations.Add(rel MPSMS)
ds_mpsms.Relations.Add(rel MPSMS)
This is what I have tried and it gives me an error saying a relationship cannot be made between tables in different datasets:
Dim da_XWALK As New OleDb.OleDbDataAdapter("SE
Dim da_mpsms As New OleDb.OleDbDataAdapter("Se
da_XWALK.Fill(ds_XWALK, "XWALKV43II")
da_mpsms.Fill(ds_mpsms, "MPSMS")
ds_XWALK.EnforceConstraint
ds_mpsms.EnforceConstraint
Dim parentCol As DataColumn = ds_XWALK.Tables("XWALKV43I
Dim childCol As DataColumn = ds_mpsms.Tables("MPSMS").C
Dim relMPSMS As DataRelation
relMPSMS = New DataRelation("MPSMS", parentCol, childCol)
ds_XWALK.Relations.Add(rel
ds_mpsms.Relations.Add(rel
Dim myRelation As New DataRelation("Data Relation 1", Column1, Column2)