C# between two dates search -
i have data (access) , using datetimepicker. have thousand datas
name: birthday: aaaa 05.02.2000 bbbb 01.08.1995 cccc 04.07.1995 dddd 05.07.1995
i use 2 datetimepicker , button, datetimepicker2 first time, datetimepicker3 last time , click filter button. want show me datagridview on forexample 01.01.1995 - 01.12.1995 born list
i used code:
private void btnfiltre_click(object sender, eventargs e) { table.clear(); oledbdataadapter adtr = new oledbdataadapter("select * data date between'" + datetimepicker2.text + "' , '" + datetimepicker3.text + "'", cnn); adtr.fill(table); datagridview1.datasource = table; }
but take warning.
what can it?
many details missing, but, usual: not use string concatenation build sql commands.
use parametrized query this
private void btnfiltre_click(object sender, eventargs e) { table.clear(); oledbdataadapter adtr = new oledbdataadapter("select * data date between ? , ?", cnn) adtr.selectcommand.parameters.addwithvalue("@p1",datetimepicker2.value) adtr.selectcommand.parameters.addwithvalue("@p2",datetimepicker3.value) adtr.fill(table); datagridview1.datasource = table; }
details missing:
- which database using? if ms-access
date
reserved keyword , need encapsulated in square brackets - where initialize connection? (do not use global connection object)
- is date field datetime or string (bad, bad)
Comments
Post a Comment