The 6th page of the OfficeTest General Tests tutorial covers manually creating and linking datasets and assigning them to a GridView. If you want to review the other tutorials, the central page can be found here Firebird and .NET 2.0 development Example.
This topic is more just for interest sake and to provide a simple example of manually manipulating datasets. I've seen other discussions on other sites related to this topic but honestly I don't see a lot of use for this functionality.
6. Manually creating and linking datasets.
Populate
The Populate() method loads the entire results of the Employee table and then the entire results of the JobPosition table. The data is loaded into separate named views within the dataset. Once the data is loaded the Relations functionality of the dataset is used to create a merged dataset which contains the Employee data with the JobPosition.JobTitle added in place of JobPositionID.
NOTE: There is a typo in the current code. The JobPositionID in the JoinedTables was suppose to be the EmployeeID. This doesn't really change anything or cause any problems.
Loading the data
DataSet myDataSet = new DataSet();
string connectionString = WebConfigurationManager.ConnectionStrings["OfficeTestFirebird"].ConnectionString;
FbConnection con = new FbConnection(connectionString);
try {
con.Open();
FbCommand cmd = new FbCommand("SELECT \"EmployeeID\", \"FirstName\", \"LastName\", \"HireDT\", \"JobPositionID\" FROM \"Employee\"", con);
FbDataReader reader = cmd.ExecuteReader();
myDataSet.Load(reader,LoadOption.OverwriteChanges,new string[]{"Employee"});
reader.Close();
cmd = new FbCommand("SELECT \"JobPositionID\",\"JobTitle\",\"JobDesc\" FROM \"JobPosition\"", con);
reader = cmd.ExecuteReader();
myDataSet.Load(reader, LoadOption.OverwriteChanges, new string[] { "JobPosition" });
reader.Close();
} finally {
con.Close();
}
DataTable employee = myDataSet.Tables["Employee"];
DataColumn employeeJobPositionID = employee.Columns["JobPositionID"];
DataTable jobPosition = myDataSet.Tables["JobPosition"];
DataColumn jobPositionJobPositionID = jobPosition.Columns["JobPositionID"];
myDataSet.Relations.Add("JobPositionEmployee", jobPositionJobPositionID, employeeJobPositionID);
Next a relation is added to the dataset linking the JobPosition.JobPositionID primary key to the Employee.JobPositionID foreign key column. An alias ("JobPositionEmployee") is assigned to the relationship.
myDataSet.Tables.Add(new DataTable("JoinedTables"));
myDataSet.Tables["JoinedTables"].Columns.Add("EmployeeID");
myDataSet.Tables["JoinedTables"].Columns.Add("FirstName");
myDataSet.Tables["JoinedTables"].Columns.Add("LastName");
myDataSet.Tables["JoinedTables"].Columns.Add("HireDT");
myDataSet.Tables["JoinedTables"].Columns.Add("JobTitle");
foreach(DataRow row in myDataSet.Tables["JobPosition"].Rows) {
DataRow[] joinRows = row.GetChildRows("JobPositionEmployee");
foreach (DataRow joinRow in joinRows) {
DataRow newRow = myDataSet.Tables["JoinedTables"].NewRow();
newRow["EmployeeID"] = joinRow["EmployeeID"];
newRow["FirstName"] = joinRow["FirstName"];
newRow["LastName"] = joinRow["LastName"];
newRow["HireDT"] = joinRow["HireDT"];
newRow["JobTitle"] = row["JobTitle"];
myDataSet.Tables["JoinedTables"].Rows.Add(newRow);
}
}
For each matching child row we simply manually assign the data to a new row we created for the "JoinedTables" table we created in the dataset. The "JobTitle" must be retrieved from the "JobPosition" table row since the child rows are from the Employee table which don't have the JobTitle.
gvEmployees.DataSource = myDataSet.Tables["JoinedTables"].DefaultView;
gvEmployees.DataBind();
Summary
This code is really very straight forward except perhaps the follow:
myDataSet.Relations.Add("JobPositionEmployee", jobPositionJobPositionID, employeeJobPositionID);
As I mentioned before there are many ways the same results could be achieved. We could have simply selected the data in that format from the database to start with. We could also just manually loop through the Employees and record by record lookup the corresponding JobPosition. In this example I only wanted to demonstrate how the DataSet Relations functionality could be used. In some cases it may also improve performance. If you already have Employee and JobPosition data in cached DataSets and don't want to retrieve a new joined result from the database this method might prove useful and faster.
See next Tutorial...Employee: Select Procedure web page which calls a stored procedure with a result set directly and through a select SQL query.