2014-02-11 39 views
1

我在數據庫中有GRP(PK),start_date,end_date中的列。 start_date/end_date有數據類型日期。如何在sql服務器中按月搜索日期c#

我有一個datagridview,一個包含12個月的組合框和一個按鈕來按月搜索記錄。怎麼做,我不知道。

有人可以給我這個想法。我想一年做同樣的事情。

謝謝。

+0

你有你的表格數據綁定到'Gridview'嗎?你是如何綁定它的? – zey

+0

在[Function Month](http://technet.microsoft.com/en-us/library/ms187813.aspx)上搜索Sql Server文檔,直接使用Sql Server Management Studio嘗試查詢,然後編寫代碼 – Steve

+0

yes,data在加載表單時在datagridview中可見 –

回答

0

您可以在查詢中使用datepart() TSQL功能, 例如,如果要加載僅在第2個月創紀錄:

SELECT * FROM leaves WHERE DATEPART(month, start_date)=2 or DATEPART(month, end_date)=2 
+0

感謝brthr的工作。 –

+0

歡迎兄弟,我很高興它幫助你 – Shaegorath

0

嘗試使用下面的代碼:

declare @StartDate date 
set @StartDate = getdate() 
select @StartDate 

Select MONTH(@StartDate) 

SELECT * 
FROM YOURTABLE 
WHERE MONTH(StartDate) = @Month 
0

如果您正在使用ado.net你應該Transact-SQL Month看一看。如果應用程序變得更復雜,我會推薦使用實體框架。

Select * from table where Month(StartDate) = @Month

+0

感謝brthr,寫什麼來代替@Month –

0

更改您的查詢像

string _query = String.Format("select * from leaves where Month(start_date) ='{0}' and Month(end_date) ='{0}'",yourComboxBoxSelectedValue); 

objcmd = new SqlCommand(_query, objc);