How to search between two dates in C# with MS Access -
so group project need able search between 2 dates "start_date" & "end_date". both of these fields use 1 column database called "study_date". can search 1 date entering specified date either field.
here code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.data.oledb; namespace rras { public partial class formrras : form { public oledbconnection dataconnection = new oledbconnection(); public formrras() { initializecomponent(); } //when form loads sets intial combo box rfr item null private void formrras_load(object sender, eventargs e) { // todo: line of code loads data 'database1dataset.tblreject_test' table. can move, or remove it, needed. this.tblreject_testtableadapter.fill(this.database1dataset.tblreject_test); cmbrfr.selecteditem = ""; } //addrfr method, called in newrfrpopup public void addrfr(object item) { cmbrfr.items.add(item); } //the code button closes application private void exittoolstripmenuitem_click(object sender, eventargs e) { this.close(); } private void btnsearch_click(object sender, eventargs e) { //this creates string publisher grabs information combo box on form. //select , dataconnection defined here. string department = string.isnullorempty(txtdepartment.text)? "%" : txtdepartment.text; string start_date = string.isnullorempty(txtstart.text)? "%" : txtstart.text; //datetime start = datetime.parse(start_date); string end_date = string.isnullorempty(txtend.text)? "%" : txtend.text; string anatomy = string.isnullorempty(txtanatomy.text)? "%" : txtanatomy.text; string rfr = string.isnullorempty(cmbrfr.text) ? "%" : cmbrfr.text; string comment = string.isnullorempty(txtcomment.text)? "%" : txtcomment.text; string select = "select * tblreject_test department_id '" + department + "'" + "and body_part_examined like'" + anatomy + "'" + "and study_date like'" + start_date + "'" + "and study_date like'" + end_date + "'" + "and reject_category like'" + rfr + "'" + "and reject_comment like'" + comment + "'"; //dataconnection connects database. string connectiontring = "provider=microsoft.jet.oledb.4.0;data source=|datadirectory|\\database1.mdb"; dataconnection = new oledbconnection(connectiontring); //the dataadapter code ensures both data in select , dataconnection strings match. oledbdataadapter rddataadapter = new oledbdataadapter(select, dataconnection); try { //it clears datagridview , loads data has been selected dataadapter. database1dataset.tblreject_test.clear(); rddataadapter.fill(this.database1dataset.tblreject_test); } catch (oledbexception exc) { system.windows.forms.messagebox.show(exc.message); } } //end of search button //temporary button thats loads chart when clicked private void btnloadchart_click(object sender, eventargs e) { { string connectiontring = "provider=microsoft.jet.oledb.4.0;data source=|datadirectory|\\database1.mdb"; dataconnection = new oledbconnection(connectiontring); try { int count = database1dataset.tables["tblreject_test"].rows.count; dataconnection.open(); oledbcommand command = new oledbcommand(); command.connection = dataconnection; string query = "select * tblreject_test"; command.commandtext = query; oledbdatareader reader = command.executereader(); while (reader.read()) { charrejections.series["rfr"].points.addxy(reader["reject_category"].tostring(), reader[count].tostring()); } dataconnection.close(); } catch (exception ex) { messagebox.show("error " + ex); } } } //end of load chart button //this button loads newrfrpopup form private void addrfrtoolstripmenuitem_click(object sender, eventargs e) { newrfrpopup popup = new newrfrpopup(this); popup.showdialog(); } } }
you can try this
string select = "select * tblreject_test department_id '" + department + "'" + "and body_part_examined like'" + anatomy + "'" + "and study_date >=#" + start_date + "#" + "and study_date <=#" + end_date + "#" + "and reject_category like'" + rfr + "'" + "and reject_comment like'" + comment + "'";
Comments
Post a Comment