如何使用Asp.net C#检索3个月大的记录?

时间:2017-06-20 04:50:24

标签: c# asp.net .net ms-access c#-4.0

我想把已经加入三个月的员工记录拉回公司,我使用accdb数据库,编码将是asp.net c#,我总是陷入检索的日期和天数记录。

 public void DisplayProbationList()
{
     DateTime today = DateTime.Today;
    DateTime Threemonthago = DateTime.Now.AddMonths(-3);
    string connectionString = GetConnectionString();
    OleDbConnection connection = new OleDbConnection();

    connection.ConnectionString = connectionString;
    connection.Open();
    OleDbCommand command = connection.CreateCommand();
    //string sql = "SELECT * FROM HolidaysList";
    //command.CommandText = sql;
    OleDbCommand Command = new OleDbCommand("SELECT * FROM EmployeeDetails where Datejoining Between '"+ Threemonthago+"' AND '"+Today+"' ", connection);
    OleDbDataAdapter OleDbDataAdapter = new OleDbDataAdapter(Command);
    DataSet ds = new DataSet();
    OleDbDataAdapter.Fill(ds);
    Probation.DataSource = ds;
    Probation.DataBind();

}

1 个答案:

答案 0 :(得分:1)

您需要Access SQL中的 octothorpes 以及日期表达式的强制格式:

OleDbCommand Command = new OleDbCommand("SELECT * FROM EmployeeDetails WHERE Datejoining BETWEEN #" + Threemonthago.ToString("yyyy'/'MM'/'dd") + "# AND #" + Today.ToString("yyyy'/'MM'/'dd") + "#", connection);

请注意,在这种情况下连接是安全的,因为值是在代码中生成的,因此不会发生注入。否则,请考虑使用参数。