For a parent list to supply its information to another list, the child list must have a column that would serve as a relay. This means that, in the child list, you must create a column that would correspond to the primary key of the parent table. This column of the child list is called a foreign key. To visually create a foreign key, in the Tables Collection Editor, you can click the ellipsis button of the Constraints field. In the Constraints Collection Editor, you can click Add -> Foreign Key Constraint. In Foreign Key Constraint:
Here is an example:
In the same way, you can create the other foreign keys for your table. The list of foreign keys would appear in the Members list of the Constraints Collection Editor. To support foreign keys, the System.Data namespace provides a class named ForeignKeyConstraint. The ForeignKeyConstraint class is derived from the Constraint class. To programmatically create a foreign key, declare a variable of type ForeignKeyConstraint and initialize it with one of its six constructors. If you want to specify (only) the names of the primary key and the foreign key columns, you can use the following constructor: public ForeignKeyConstraint(DataColumn parentColumn, DataColumn childColumn); Here is an example: 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;
namespace Exercise1
{
public partial class Exercise : Form
{
DataColumn colGenderID;
UniqueConstraint PKGenderID;
DataColumn colGender;
DataColumn colStudentGenderID;
DataTable tblGenders;
DataColumn colStudentID;
DataColumn colUsername;
DataTable tblStudents;
UniqueConstraint cnsUniqueUsername;
ForeignKeyConstraint FKGenderID;
DataSet dsStudents;
public Exercise()
{
InitializeComponent();
}
private void Exercise_Load(object sender, EventArgs e)
{
colGenderID = new DataColumn("GenderID", Type.GetType("System.Int32"));
colGenderID.AutoIncrement = true;
colGenderID.AutoIncrementSeed = 1;
colGenderID.AutoIncrementStep = 1;
colGender = new DataColumn("colGender", Type.GetType("System.String"));
tblGenders = new DataTable("Gender");
tblGenders.Columns.Add(colGenderID);
tblGenders.Columns.Add(colGender);
colStudentID = new DataColumn("ColumnID",
Type.GetType("System.Int32"));
colStudentID.Unique = true;
colStudentID.AutoIncrement = true;
colStudentID.AutoIncrementSeed = 1000;
colStudentID.AutoIncrementStep = 5;
colUsername = new DataColumn("Username", Type.GetType("System.String"));
colStudentGenderID = new DataColumn("GenderID", Type.GetType("System.Int32"));
tblStudents = new DataTable("Student");
tblStudents.Columns.Add(colStudentID);
tblStudents.Columns.Add(colUsername);
tblStudents.Columns.Add(colStudentGenderID);
cnsUniqueUsername = new UniqueConstraint("UniqueUsername", colUsername);
tblStudents.Constraints.Add(cnsUniqueUsername);
PKGenderID = new UniqueConstraint("PKGenderID", colGenderID, true);
tblGenders.Constraints.Add(PKGenderID);
FKGenderID = new ForeignKeyConstraint(colGenderID, colStudentGenderID);
tblStudents.Constraints.Add(FKGenderID);
dsStudents = new DataSet("Students");
dsStudents.Tables.Add(tblStudents);
dsStudents.Tables.Add(tblGenders);
}
}
}
A relational database is an application in which different tables work together so that information in one table can be made available to other tables. To make this possible, you start by creating the tables as we have done above. Each table must have a primary key. As we saw above, to make data from a parent table available to data from a child table, the child table must have a foreign key that would "represent" the information from the parent table. Once the tables and their keys have been created, you can link them.
To visually create a relationship in a data set, first select the DataSet object. In the Properties window, click Relations and click its ellipsis button. In the Relations Collection Editor, to create a relationship, click Add. In the Relation dialog box, you can complete the text boxes and combo boxes using the same options as when creating a foreign key. If you had previously created a(the) foreign key(s) in your table, the relationship(s) would automatically be created and configured so you do not have to recreate it(them). To support relations in a database, the DataSet class is equipped with a property named Relations. The DataSet.Relations property is an object of type DataRelationCollection. The DataRelationCollection class is a collection of objects where each member is of type DataRelation. To create a relationship, declare a variable of type DataRelation and initialize it using one of its six constructors. To specify the primary key and the foreign key, you can use the following constructor: public DataRelation(string relationName, DataColumn parentColumn, DataColumn childColumn) The first argument is the name of the relationship. The second argument is the column name of the primary key. The last argument is the column name of the foreign key. After creating the relationship, you can add it to the DataSet.Relations property. To support this, the DataRelationCollection class is equipped with the Add() method that is provided in various versions. One of the versions uses the following syntax: public void Add(DataRelation relation); Here is an example 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;
namespace Exercise1
{
public partial class Exercise : Form
{
DataColumn colGenderID;
UniqueConstraint PKGenderID;
DataColumn colGender;
DataColumn colStudentGenderID;
DataTable tblGenders;
DataColumn colStudentID;
DataColumn colUsername;
DataTable tblStudents;
UniqueConstraint cnsUniqueUsername;
ForeignKeyConstraint FKGenderID;
DataRelation relSchool;
DataSet dsStudents;
public Exercise()
{
InitializeComponent();
}
private void Exercise_Load(object sender, EventArgs e)
{
colGenderID = new DataColumn("GenderID", Type.GetType("System.Int32"));
colGenderID.AutoIncrement = true;
colGenderID.AutoIncrementSeed = 1;
colGenderID.AutoIncrementStep = 1;
colGender = new DataColumn("colGender", Type.GetType("System.String"));
tblGenders = new DataTable("Gender");
tblGenders.Columns.Add(colGenderID);
tblGenders.Columns.Add(colGender);
colStudentID = new DataColumn("ColumnID",
Type.GetType("System.Int32"));
colStudentID.Unique = true;
colStudentID.AutoIncrement = true;
colStudentID.AutoIncrementSeed = 1000;
colStudentID.AutoIncrementStep = 5;
colUsername = new DataColumn("Username", Type.GetType("System.String"));
colStudentGenderID = new DataColumn("GenderID", Type.GetType("System.Int32"));
tblStudents = new DataTable("Student");
tblStudents.Columns.Add(colStudentID);
tblStudents.Columns.Add(colUsername);
tblStudents.Columns.Add(colStudentGenderID);
cnsUniqueUsername = new UniqueConstraint("UniqueUsername", colUsername);
tblStudents.Constraints.Add(cnsUniqueUsername);
PKGenderID = new UniqueConstraint("PKGenderID", colGenderID, true);
tblGenders.Constraints.Add(PKGenderID);
FKGenderID = new ForeignKeyConstraint(colGenderID, colStudentGenderID);
tblStudents.Constraints.Add(FKGenderID);
dsStudents = new DataSet("Students");
dsStudents.Tables.Add(tblStudents);
dsStudents.Tables.Add(tblGenders);
relSchool = new DataRelation("SchoolRelations", colGenderID, colStudentGenderID);
dsStudents.Relations.Add(relSchool);
}
}
}
There are many other ways you can create a relationship.
Once a relationship exists between two tables, you can use that relationship to have the information flow from one list to another. To support this, the visual controls of the .NET Framework are equipped with various properties, including DataSource and DisplayMember. The DataSource property specifies the name of the data set variable that holds the database. The DisplayMember property specifies the name of the column from the table that has the value to be displayed. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|