Why abandon inner join and use datarelation object to create a relationship between two tables.

Source: Internet
Author: User

(Domestic version)



Generally, we use inner join to connect data from different tables, and the resulting data is a row. If BIND is sent to the DataGrid Control, not that nice! Instead, if we use datarelation to replace this inner join and bind it to the DataGrid Control, it will have a better interface effect (see tables ).

The following describes how to use datarelation objectCode:

' Declare Variables

Dim CN As Sqlconnection

Dim Da1 As Sqldataadapter

Dim Da2 As Sqldataadapter

Dim DS As Dataset

Dim Parentcolumn As Datacolumn

Dim Childcolumn As Datacolumn

Dim Dr As System. Data. datarelation


Try

' Instantiate sqlconnection object

CN =   New Sqlconnection ( " Data Source = localhost; initial catalog = northwind; Integrated Security = true; " )

CN. open ()


' Instantiate the data of an individual sqldataadapter In the MERs table

Da1 =   New Sqldataadapter ( " Select * from MERs " , CN)

Da2 =   New Sqldataadapter ( " Select * from orders " , CN)

' Instantiate DataSet object

DS =   New Dataset


' Store the read data in dataset.

Da1.fill (DS, " MERs " )

Da2.fill (DS, " Orders " )


' Create linking column in datarelation

Parentcolumn = DS. Tables ( " MERs " ). Columns ( " Customerid " )

Childcolumn = DS. Tables ( " Orders " ). Columns ( " Customerid " )


' Instantiate datarelation object

Dr =   New System. Data. datarelation ( " Detail-orders " , Parentcolumn, childcolumn)

DS. relations. Add (DR)


' Bind the data in dataset to the DataGrid Control.

Datagrid1.datasource = DS


Catch Ex As Sqlexception

'

MessageBox. Show (ex. Message, " Error " , Messageboxbuttons. OK, messageboxicon. Exclamation)


End   Try

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.