Microsoft Visual C# ADO.NET Programming: Data Joins
A data join is a technique of creating a list of records from more than one table, using all columns from all tables involved, or selecting only the…
Joins Fundamentals#
Introduction#
A data join is a technique of creating a list of records from more than one table, using all columns from all tables involved, or selecting only the desired columns from one or all of the tables involved. This means that a data join is essentially created in three steps:
- Selecting the tables that will be involved in the join
- Selecting a column that will create the link in each table
- Writing or creating a SQL statement that will produce the records
Practical Learning: Introducing Joins
- Start Microsoft Visual Studio and create a new Windows Application named SouthernEnergyCorp1
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type SouthernEnergyCorp.cs and press Enter
The Tables of a Join#
Before creating a join, you must have the tables that would be involved. The tables are created using the techniques we have seen in previous lessons. It is also important to create a primary key for each table. The parent table would usually need only this primary key that would be used to "link" it to a child table. When creating the child table, remember to create a column that would serve as the link to the parent table. By a (good) habit as we saw when studying relationships, the name and the data type of this column is the same as the primary key of the parent table. Practical Learning: Creating the Tables
- Double-click the middle of the form and change the file as follows: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace SouthernEnergyCorp1 { public partial class SouthernEnergyCorp : Form { string[] strCode; public SouthernEnergyCorp() { InitializeComponent(); } void CreateDatabase() { using (SqlConnection cntSEC = new SqlConnection("Data Source=(local);" + "Integrated Security=Yes")) { SqlCommand cmdSEC = new SqlCommand("IF EXISTS (" + "SELECT name " + "FROM sys.databases " + "WHERE name = N'SouthernEnergyCorp1')" + "DROP DATABASE SouthernEnergyCorp1; " + "CREATE DATABASE SouthernEnergyCorp1;", cntSEC); cntSEC.Open(); cmdSEC.ExecuteNonQuery(); MessageBox.Show("The SouthernEnergyCorp1 database has been created.", "Southern Energy Corporation", MessageBoxButtons.OK, MessageBoxIcon.Information); } using (SqlConnection cntSEC = new SqlConnection("Data Source=(local);" + "Database='SouthernEnergyCorp1';" + "Integrated Security=Yes")) { SqlCommand cmdSEC = new SqlCommand("CREATE TABLE Customers(" + "AccountNumber nvarchar(10) not null, CustomerName nvarchar(50) not null," + "ResidenceType nvarchar(40), Address1 nvarchar(60) not null, " + "Address2 nvarchar(50), City nvarchar(50), State nvarchar(40), " + "ZIPCode nvarchar(20), Constraint PK_Customers Primary Key(AccountNumber));", cntSEC); cntSEC.Open(); cmdSEC.ExecuteNonQuery(); MessageBox.Show("The Customers table has been created.", "Southern Energy Corporation", MessageBoxButtons.OK, MessageBoxIcon.Information); } using (SqlConnection cntSEC = new SqlConnection("Data Source=(local);" + "Database='SouthernEnergyCorp1';" + "Integrated Security=Yes")) { SqlCommand cmdSEC = new SqlCommand("CREATE TABLE MetterReadings(" + "ReadingID int identity(1, 1) not null, " + "CustomerAccount nvarchar(10) not null, " + "PreviousReadingDate date," + "PreviousReadingNumber int, NewReadingDate date not null, " + "NewReadingNumber int not null," + "Consumption AS (NewReadingNumber - PreviousReadingNumber), " + "Constraint PK_MetterReadings Primary Key(ReadingID));", cntSEC); cntSEC.Open(); cmdSEC.ExecuteNonQuery(); MessageBox.Show("The MetterReadings table has been created.", "Southern Energy Corporation", MessageBoxButtons.OK, MessageBoxIcon.Information); } using (SqlConnection cntSEC = new SqlConnection("Data Source=(local);" + "Database='SouthernEnergyCorp1';" + "Integrated Security=Yes")) { SqlCommand cmdSEC = new SqlCommand("INSERT INTO Customers VALUES" + "('27397405', 'John Melmann', 'Single Family', '12005 Belcrest Ave', NULL, " + "'Takoma Park', 'MD', '20904')," + "('80940696', 'Daniel Fletcher', 'Apartment', '808 Solthan Drive N.E.', '#206', " + "'Washington', 'DC', '20012'), " + "('20499507', 'Frank Donaldson', 'Single Family', '2466 Nicholson Crt', NULL, " + "'Alexandria', 'VA', '22234')," + "('92824975', 'Gabriela Dawson', 'Townhouse', '1602 Salomons Ave', NULL, " + "'Silver Spring', 'MD', '20906');", cntSEC); cntSEC.Open(); cmdSEC.ExecuteNonQuery(); MessageBox.Show("A few records have been added to the Customers table.", "Southern Energy Corporation", MessageBoxButtons.OK, MessageBoxIcon.Information); } using (SqlConnection cntSEC = new SqlConnection("Data Source=(local);" + "Database='SouthernEnergyCorp1';" + "Integrated Security=Yes")) { SqlCommand cmdSEC = new SqlCommand("INSERT INTO MetterReadings(CustomerAccount, " + "PreviousReadingDate, PreviousReadingNumber, NewReadingDate, NewReadingNumber) VALUES" + "('27397405', '20110415', 2488, '20110512', 2646), " + "('80940696', '20110415', 602, '20110512', 648), " + "('20499507', '20110415', 4155, '20110513', 4402), " + "('92824975', '20110415', 1904, '20110513', 2006), " + "('27397405', '20110512', 2646, '20110616', 2960), " + "('80940696', '20110512', 648, '20110616', 669), " + "('20499507', '20110513', 4402, '20110616', 4881);", cntSEC); cntSEC.Open(); cmdSEC.ExecuteNonQuery(); MessageBox.Show("A few records have been added to the MetterReadings table.", "Southern Energy Corporation", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void SouthernEnergyCorp_Load(object sender, EventArgs e) { CreateDatabase(); } } }
- Execute the application to create the database
- Click OK for each message box
- Close the form and return to your programming environment
- Change the Load event as follows: private void SouthernEnergyCorp_Load(object sender, EventArgs e) { // CreateDatabase(); strCode = new string[] { }; }
- Return to the form and design it as follows:

