如何从日期为最后一天的表中选择多列?
Posted
技术标签:
【中文标题】如何从日期为最后一天的表中选择多列?【英文标题】:How can I select multi-column from table where date is last day? 【发布时间】:2015-05-02 06:15:02 【问题描述】:我正在尝试从表中选择日期小于今天的字段。
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;
这会产生data type miss
或missing operator
错误,使用 Access DB。
【问题讨论】:
我给出了答案,但我不明白您使用的是什么数据库。是 mysql(标签)还是 MS Access? 【参考方案1】:您正在将 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 > ?";
【讨论】:
我用的是 Access 数据库,非常感谢,效果很好以上是关于如何从日期为最后一天的表中选择多列?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Python 仅查找包含该月最后一天的日期的文件?