如何从日期为最后一天的表中选择多列?

时间:2015-05-02 06:15:03

标签: c# xcode ms-access oledb oledbcommand

我正在尝试从我的表中选择一个日期少于今天的字段。

connect.Open();
command.Connection = connect;
today = DateTime.Today.ToString("dd/MM/yyyy");
string query = "select attendance as [Attend], Emp_UserId as ID, 'Date_ofday' as [Today Date] , Emp_UserName as Name ,Delay_Hours as [Delay Hours] from Attendance where Date_ofday > '"+ DateTime.Parse(today) + "'  ";
command.CommandText = query;
OleDbDataAdapter da1 = new OleDbDataAdapter(command);
DataTable dt1 = new System.Data.DataTable();
da1.Fill(dt1);
dataGridView1.DataSource = dt1;

使用Access DB会出现data type missmissing operator错误。

1 个答案:

答案 0 :(得分:0)

您正在将DateTime转换为字符串,然后在下一行将其解析回DateTime,这没有意义。我认为你应该使用参数,看看这是否有效,我不使用MySql:

connect.Open();
command.Connection = connect;
todday = DateTime.Today.ToString("dd/MM/yyyy");
string query = "select attendance as [Attend], Emp_UserId as ID, 'Date_ofday' as [Today Date] , Emp_UserName as Name ,Delay_Hours as [Delay Hours] from Attendance where Date_ofday > @Today";
command.CommandText = query;
OleDbDataAdapter da1 = new OleDbDataAdapter(command);
DataTable dt1 = new System.Data.DataTable();
da1.SelectCommand.Parameters.AddWithValue("@Today", DateTime.Today);
da1.Fill(dt1);
dataGridView1.DataSource = dt1;

如果您使用MS Access,查询将变为:

string query = "select attendance as [Attend], Emp_UserId as ID, 'Date_ofday' as [Today Date] , Emp_UserName as Name ,Delay_Hours as [Delay Hours] from Attendance where Date_ofday > ?";