John Verber
John Verber

Reputation: 755

MS Access query code in C#

I'm trying to run a query on an Access DB, I'm used to SQL queries but this doesn't seem to be working the same way. Here's my query:

OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "SELECT RecordID, TimeStamp, EmployeeName, AreaDescription       FROM LoginRecords r, Employees e, Areas a WHERE((e.EmployeeID = r.EmployeeID) && (a.AreaID =r.AreaID) && (TimeStamp > startDate AND < endDate)) ORDER BY TimeStamp;"

I can't seem to get this to run but technically from a SQL standpoint this should be a good query. The tables are LoginRecords, Employees, Areas. I can load the tables if that would be helpful. I appreciate any feedback as to why this won't work in Access. And startDate and endDate are variables from user input boxes.

Upvotes: 0

Views: 404

Answers (1)

John Woo
John Woo

Reputation: 263723

Try this one,

This is SQL-92

SELECT      RecordID, 
            TimeStamp, 
            EmployeeName, 
            AreaDescription       
FROM        LoginRecords r
                INNER JOIN Employees e
                    ON e.EmployeeID = r.EmployeeID
                INNER JOIN Areas a 
                    ON a.AreaID = r.AreaID
WHERE   TimeStamp > startDate AND
        TimeStamp < endDate
ORDER BY TimeStamp;

Use SQL-92 format rather SQL-89 format because SQL-89 (aside from old style) is prone to going CROSS JOIN if not handled correctly.

and this is SQL-89

SELECT      RecordID, 
            TimeStamp, 
            EmployeeName, 
            AreaDescription       
FROM        LoginRecords r, Employees e, Areas a
WHERE   (e.EmployeeID = r.EmployeeID) AND
        (a.AreaID = r.AreaID) AND
        (TimeStamp > @startDate AND
         TimeStamp < @endDate)
ORDER BY TimeStamp;

MSACCESS: INNER JOIN, OUTER JOIN (LEFT and RIGHT)

Upvotes: 1

Related Questions