C# SQL Server中的Select语句

C# SQL Server中的Select语句,c#,sql,database,C#,Sql,Database,我正在尝试显示两个日期之间的学生个人出勤记录。当教师选择两个日期时,记录将显示在GridView控件中。然而,我有一个问题,其他学生的出勤记录也被显示出来。我不能只显示学生出勤记录 这是我的C代码,当老师点击查看按钮时: protected void ButtonView_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); con.ConnectionString = "Data

我正在尝试显示两个日期之间的学生个人出勤记录。当教师选择两个日期时,记录将显示在GridView控件中。然而,我有一个问题,其他学生的出勤记录也被显示出来。我不能只显示学生出勤记录

这是我的C代码,当老师点击查看按钮时:

protected void ButtonView_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=DESKTOP-H7KQUT1;Initial Catalog=SAOS;Integrated Security=True";
    con.Open();

    string query1 = "SELECT * FROM attendance WHERE Date between '" + datepicker.Text + "'AND'" + datepicker1.Text + "'";
    SqlCommand sqlcomm = new SqlCommand(query1, con);

    SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
    DataTable dt = new DataTable();
    sda.Fill(dt);

    SqlDataReader sdr = sqlcomm.ExecuteReader();

    if (sdr.Read())
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    else
    {
        Label7.Text = "No records found!!";
    }

    con.Close();
}
问题在于:

 string query1 = "SELECT * FROM attendance WHERE Date between '" + datepicker.Text + "'AND'" + datepicker1.Text + "'";

我有一个名为
Student\u ID
的列作为考勤表的外键。

显示示例数据、预期输出和表名,以及在临时查询中使用concate的危险性。若要更改where子句以按Student\u ID进行限制?不能仅显示学生考勤记录-->为此,您需要将Student\u ID传递给查询只显示与他/她相关的数据-您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-检查show sample data和预期的输出和表名以及在临时查询中使用concate的危险性将where子句更改为按student\u ID限制?不能仅显示student考勤记录-->为此,您需要将student_id传递给查询,以仅显示与他/她相关的数据-不应将SQL语句串联在一起-使用参数化查询,以避免SQL注入-签出
string query1 = "SELECT * FROM attendance WHERE Date between @startDate AND @endDate and Student_ID = @studentId";

SqlCommand sqlcomm = new SqlCommand(query1, con);
sqlcomm.Parameters.Add("@startDate", SqlDbType.Date).Value = datepicker.Text;
sqlcomm.Parameters.Add("@endDate", SqlDbType.Date).Value = datepicker1.Text;
sqlcomm.Parameters.Add("@studentId", SqlDbType.Int).Value = 100;