我有一个dateTimePicker,我希望DGV填充一个查询,该查询从DB(SQL CE)中选择与dateTimePicker上所选月份对应的所有条目。
这将是逻辑
SELECT FROM Employees WHERE (@MonthEnd or @MonthEnd2) = dateTimePicker1.SelectedMonth
@MonthEnd或@ MonthEnd2的值是datetime。
我如何首先在dateTimePicker上选择当月?
我应该将datetime从DB转换为字符串吗?还是int?那么我该如何只选择数据的月份部分来与日历的选定月份进行比较呢?
编辑:感谢@Hogan,我能够提出这样的代码:(希望它对有需要的人有所帮助,就像我一样(我要求提供日期时间选择器,但改为使用日历)。)string constringF = @"Data Source=|DataDirectory|\cadastramentodb.sdf;Persist Security Info=False";
string QueryF = "select * from Funcionarios where datepart(month, datafimcontrato) = @mes ";
SqlCeConnection conDataBaseF = new SqlCeConnection(constringF);
SqlCeCommand cmdDataBaseF = new SqlCeCommand(QueryF, conDataBaseF);
cmdDataBaseF.Parameters.Add("@mes", SqlDbType.Int).Value = Convert.ToInt32(monthCalendar1.SelectionStart.Month);
答案 0 :(得分:0)
有一个名为month的函数。您可以像这样使用它:
SELECT *
FROM Emp
WHERE MONTH(datefieldintable) = @MonthInput