简体   繁体   English

如何在c#中将加号之间的日期作为一个命令使用

[英]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: 两者之间的日期有效,但是当我添加LEFT JOIN ,它会显示错误:

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. 您必须保持SQL语句的顺序。 First the join , then where -clause. 首先是join ,然后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 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 所以CommandText初始化将如下所示

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. WHERE子句应始终位于JOIN语句之后。 Also make sure that you should use an Alias names while dealing with JOIN Statements in an SQL queries for better understanding. 还要确保在SQL查询中处理JOIN语句时应使用别名,以便更好地理解。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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