I have been tasked with writing a winforms c# application that allows the users to run ad-hoc queries. I have searched online and found a lot of tools that can be purchased (EasyQuery) but at this time buying it is out of the question.
So I am trying to write this myself. At this point I have created a treeview that is populated at run-time with the tables/columns and I can select the columns that are checked by the users. But now I have to figure out how to dynamically JOIN
the tables that they have selected.
Partial table structure is below:
Table - Roles - PK = RoleId
RoleId
RoleName
Table - Center PK = CenterId/RoleId/Prefix
CenterId
RoleId
Prefix
Table - Employee - PK = EmployeeID
EmployeeId
Name
CenterId
Dept
Org
Table - Prof - PK = EmployeeId/Profile
EmployeeId
Profile
I have a total of 6 tables that can be joined on a variety of fields, but since the users need to join on the fly I need to determine the JOIN
when they want to generate the SQL. At this point I don't know the best way to proceed to generate these JOINs
.
I even thought about creating a table in the database with the JOINs
listed for each table/column and then I could just build it from there but I am at a loss.
I have also tried something similar to this but I don't want to start down the wrong path if there are suggestions for a different way:
private void GetJoins()
{
string joinList = string.Empty;
foreach (TreeNode tn in TablesTreeView.Nodes)
{
if (tn.Checked)
if (tn.Nodes.Count > 0) // this would be parent items only
{
foreach (TreeNode childNode in tn.Nodes)
{
// for first child checked
// check the next checked parent nodes for matching checked fields
// if next parent node has same field name checked then add the JOIN
}
}
}
}
Does this seem like it is on the right track or can you suggest another way?
I am aware that this is going to be a incredibly difficult task and I haven't even gotten to the dynamic WHERE
clause yet. I am just looking for advice on the best way to create the JOINs
on an ad-hoc basis.