c# - Access Multiple search filter -
i trying click search button multiple inputs on textbox. have looked around , tried different methods somehow didn't work out. below code click event:
private void btn_table_click(object sender, eventargs e) { try { connection.open(); oledbcommand command = new oledbcommand(); command.connection = connection; string query = "select [name],[sex],[number] recordssheet [name] like('" + textbox1.text + "%'),[sex]=('" + textbox2.text + "%'),[number]=('" + textbox3.text + "%'"); command.commandtext = query; oledbdataadapter da = new oledbdataadapter(command); datatable dt = new datatable(); da.fill(dt); datagridview1.datasource = dt; connection.close(); } catch (exception ex) { messagebox.show("error " + ex); } }
you mixing equals , syntax on same operations, going result in incorrectly formed queries, absence of clause use them.
use parameterization
if have specific search term, consider adding in parameter section predefined within query :
// add properties using parameters var query = "select [name],[sex],[number] recordssheet [name] ? , [sex] ?,[number] ?";
then add parameters along necessary wildcards build query :
oledbdataadapter da = new oledbdataadapter(command); // set parameters da.selectcommand.parameters.addwithvalue("p1",textbox1.text + "*"); da.selectcommand.parameters.addwithvalue("p2",textbox2.text + "*"); da.selectcommand.parameters.addwithvalue("p3",textbox3.text + "*");
this approach not lead resolving issues related syntax, should keep protected nasty things sql injection attacks.
Comments
Post a Comment