我在Visual Studio 2008中有一个程序
一切正常,但我无法按日期搜索datagridview
我需要能够输入年份,然后将显示该年份的每个记录
我使用tableadapter
添加了按名称和姓氏搜索的搜索buttons
,但是当我对日期执行相同的操作时,它会显示“没有给出一个或多个必需参数的值”。
我使用datetime picker
来获取日期的值。
我试过的代码是
SELECT
memberno, firstname, middlename, lastname, gender, emailadd, address, phone, datejoined
FROM
Members
WHERE
(DATEPART(y, datejoined) = ?)
答案 0 :(得分:0)
您可以使用datediff
功能,如下所示
SELECT DATEDIFF(year, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
结合它
SELECT
memberno, firstname, middlename, lastname, gender,
emailadd, address, phone, datejoined
FROM
Members
WHERE
DATEDIFF(year, '2005-12-31 23:59:59.9999999', yourdate)=0;
我从文本框中获取值(您要搜索的年份)然后您可以这样做
SELECT
memberno, firstname, middlename, lastname, gender,
emailadd, address, phone, datejoined
FROM
Members
WHERE
DATEDIFF(year, datejoined, textboxvalue)=0;
int Year=Int32.Prase(textbox1.text);
var date1 = new DateTime(Year, 1, 1, 4, 5, 6);
var date2=new DateTime(year+1,1,1,4,5,6);
date >= date1 and < date2