How can I use a single SQL string to create a data-set from multiple tables in C#?
-
24-06-2021 - |
Question
I'm new to C#, and I'm kinda clueless about how to use a single sql-string to retrieve data from multiple tables (3 of them, actually).
basically there are 2 master-files:
- Task_Information, Emp_Information
And 1 transaction-file:
- Assignments: this one gets updated by the primary keys of the 2 master-files and a few other fields.
And that's alright. But now i need to run a command that will retrieve data from ALL 3 tables based on a search-parameter entered by the user, and display selected fields in all of them. in ms access, all i had to do was make a query - here's the generated sql:
SELECT Assignments.Task_No, Assignments.Assignment_No, Assignments.Assignment_Date,
Task_Information.Client_Name, Emp_Information.F_Name, Emp_Information.L_Name
FROM Emp_Information
INNER JOIN (Task_Information
INNER JOIN Assignments ON Task_Information.Task_No = Assignments.Task_No)
ON Emp_Information.Emp_ID = Assignments.Assignee
WHERE (((Assignments.Assignment_Date)="this is just some date the user has to enter..."))
In short, I need to find out how to use the same sql-string in a C# program where the user types the search parameter and clicks a button. btw, it's got to be done with an oledbdatareader/adapter;
Solution
Try this query:
SELECT Assignments.Task_No, Assignments.Assignment_No, Assignments.Assignment_Date,
Task_Information.Client_Name, Emp_Information.F_Name, Emp_Information.L_Name
FROM Emp_Information
INNER JOIN Task_Information
ON Task_Information.Task_No = Assignments.Task_No
INNER JOIN Assignments
ON Emp_Information.Emp_ID = Assignments.Assignee
WHERE (((Assignments.Assignment_Date) like "%this is just some date the user has to enter...%"))
OTHER TIPS
if you would work with an SQL Server you could use the SqlCommand
. With this you would replace your string "this is..."
with a parameter placeholder @date
, which could then be set like shown on MSDN.
The main problem here would be the usage of the OleDbDataReader
and if it supports this kind of query. But after reading the MSDN for OleDbCommand.Parameters
it should work.