Solved

dataset relations (vb.net)

Posted on 2003-11-12
11
2,402 Views
Last Modified: 2013-12-25
How can I import my database-relations in dataset-relations.
Now I typ them in as follows :
dim ds as new dataset
...
ds.relations.add("Order_detail", ds.tables("Order".colums("sernr"), ds.tables("Detail").colums("Order_sernr"))

But the same information already exists in my SQLserver !

Thanks.
0
Comment
Question by:dekeyzer
  • 6
  • 5
11 Comments
 
LVL 3

Accepted Solution

by:
SQLMaster earned 500 total points
ID: 9732511

To populate a DataSet with existing primary key constraint information from a data source, you have to call FillSchema method of the DataAdapter, or set the MissingSchemaAction property of the DataAdapter to AddWithKey before calling Fill. Please remember that foreign key constraint information is not included and will need to be created explicitly :-(

Thanks
0
 

Author Comment

by:dekeyzer
ID: 9738305
Ok, the primary key i have now but the relation not.
The Table object has the property's Parentrelation and childrelation but these are not filled in.
Any idee how i can populate them ?
0
 
LVL 3

Expert Comment

by:SQLMaster
ID: 9738476

parentRelations and childRelations are read only properties and they can only be read.

After you create relationships in DataSet, you can use these properties to get the required relations
0
 

Author Comment

by:dekeyzer
ID: 9738587
Can I find my database relations in vb so that i can create the dataset relations with that data.
I want to create the relationships in the dataset with my sql-datarelation information.
In VB6 i can get that information via ado.connection.openschema(adSchemaForeignKeys, Array(Empty, Empty, TableName)).
I don't know if that is possible in ado.net.
0
 
LVL 3

Expert Comment

by:SQLMaster
ID: 9739241
You can use DataTable.constraints property to see foreign and primary key relationships. This will return a constraintscollection that you can iterate to get the required info

Cheers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dekeyzer
ID: 9740180
Thanks, now i can make my primarykey constraints but the foreignkey constraints are missing.
Any idee how i can retrieve these form my SQLserver ?
0
 
LVL 3

Expert Comment

by:SQLMaster
ID: 9740699
Use SQLDataAdapter class for connecting to SQL Server. You should get your foreign key constraints in ConstraintsCollection
0
 

Author Comment

by:dekeyzer
ID: 9745759
I use the SQLdataadapter for connecting the SQLserver but no foreignkeyconstrains ar in the collection.
Here folows my code :

        Dim da as SqlDataAdapter
        Dim ds as DataSet
        Dim da2 As SqlDataAdapter
        Dim oTable As DataTable
        Dim oConstraint As Constraint
        Dim oColumn As DataColumn
        Dim oFK As ForeignKeyConstraint
        Dim oUK As UniqueConstraint

        sConn = "....."
        da = New SqlDataAdapter
        da2 = New SqlDataAdapter
        ds = New DataSet
        da.SelectCommand = New SqlCommand("Select * from kop")
        da.SelectCommand.Connection = New SqlConnection(sConn)
        da.MissingSchemaAction = MissingSchemaAction.AddWithKey
        da2.SelectCommand = New SqlCommand("Select * from det")
        da2.SelectCommand.Connection = New SqlConnection(sConn)
        da2.MissingSchemaAction = MissingSchemaAction.AddWithKey
        da.Fill(ds, "Order")
        da2.Fill(ds, "Detail")
        oTable = ds.Tables("Order")
        For Each oConstraint In oTable.Constraints
            MessageBox.Show(oConstraint.ConstraintName & " - " & oConstraint.ToString)
            If TypeOf oConstraint Is ForeignKeyConstraint Then
                oFK = CType(oConstraint, ForeignKeyConstraint)
                For Each oColumn In oFK.Columns
                    MessageBox.Show(oColumn.ColumnName())
                Next
            End If
            If TypeOf oConstraint Is UniqueConstraint Then
                oUK = CType(oConstraint, UniqueConstraint)
                For Each oColumn In oUK.Columns
                    MessageBox.Show(oColumn.ColumnName())
                Next
            End If
        Next

What do I wrong ?
0
 

Author Comment

by:dekeyzer
ID: 9745773
Sorry in the code above must the line
  da.SelectCommand = New SqlCommand("Select * from kop")
  da.SelectCommand = New SqlCommand("Select * from Order")
and
 da2.SelectCommand = New SqlCommand("Select * from det")
 da2.SelectCommand = New SqlCommand("Select * from Detail")
0
 
LVL 3

Expert Comment

by:SQLMaster
ID: 9746863
Sorry it is not possible to get foreign key constraints :-(
0
 

Author Comment

by:dekeyzer
ID: 9769618
I found an solution : the table sysforeignkeys has all the information that i need.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now