简体   繁体   中英

how to use Date between plus Join as one command in c#

This is my code. Date between is working, but when I add the LEFT JOIN , it shows the error:

syntax error near word left

        cn.Open();
        SqlCommand cmd = cn.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT FirstName,LastName,Date,Time,Day from 
        Attendance where Sn=@Sn and ([Date] between @SD and @ED) left JOIN 
        EmployeeTable ON Attendance.EmployeeID = EmployeeTable.EmployeeID 
        Order by Attendance.AttendanceID";
        cmd.Parameters.Add(new SqlParameter("@Sn", txtSn.Text));
        cmd.Parameters.Add(new SqlParameter("@SD", DTPStart.Text));
        cmd.Parameters.Add(new SqlParameter("@ED", DTPEnd.Text));
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt);
        dataGridView2.DataSource = dt;
        cn.Close();

You have to keep the order of the SQL statement. First the join , then where -clause.

change

SELECT FirstName,LastName,Date,Time,Day from 
Attendance where Sn=@Sn and ([Date] between @SD and @ED) left JOIN 
EmployeeTable ON Attendance.EmployeeID = EmployeeTable.EmployeeID 
Order by Attendance.AttendanceID

to:

SELECT FirstName,LastName,Date,Time,Day from 
Attendance  left JOIN EmployeeTable ON Attendance.EmployeeID = EmployeeTable.EmployeeID 
where Attendance.Sn=@Sn and (Attendance.[Date] between @SD and @ED)
Order by Attendance.AttendanceID

https://technet.microsoft.com/en-us/library/ms190617(v=sql.105).aspx

It is because of the place you are using the join. it should be after from and before where, also try to make use of alias name while you are dealing with more than one table. so the CommandText initialization will looks like the following

cmd.CommandText = "SELECT b.FirstName,b.LastName,a.Date,a.Time,a.Day" +
                  " from Attendance a" +
                  " left JOIN EmployeeTable b ON a.EmployeeID = b.EmployeeID"  +
                  " where a.Sn=@Sn and (a.[Date] between @SD and @ED)" +
                  " Order by a.AttendanceID";

The error you are getting is because you did not write query in an proper way. The WHERE Clause should always be after the JOIN Statements. Also make sure that you should use an Alias names while dealing with JOIN Statements in an SQL queries for better understanding.

I have edited your code, you can replace it with existing:

cn.Open();
SqlCommand cmd = cn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT et.FirstName,et.LastName,at.Date,at.Time,at.Day from 
Attendance at left JOIN 
EmployeeTable et ON at.EmployeeID = et.EmployeeID 
where at.Sn=@Sn and (at.[Date] between @SD and @ED)
Order by Attendance.AttendanceID";
cmd.Parameters.Add(new SqlParameter("@Sn", txtSn.Text));
cmd.Parameters.Add(new SqlParameter("@SD", DTPStart.Text));
cmd.Parameters.Add(new SqlParameter("@ED", DTPEnd.Text));
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dataGridView2.DataSource = dt;
cn.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM