Tuesday, 15 July 2014

c# - How to return a datatable from a database table, not using adapter.fill -



c# - How to return a datatable from a database table, not using adapter.fill -

i wish homecoming datatable not using adapter.fill technique.

internal datatable connectedselect(string tablename, string[] cols, string condition) { datatable dt = new datatable(); using (sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["myconnectionstring"].connectionstring)) { con.open(); using (sqlcommand cmd = new sqlcommand(generateselectstatment(tablename, cols, condition),con)) { sqldatareader rdr = cmd.executereader(); object[] temp = new object[rdr.fieldcount]; while (rdr.read()) { rdr.getvalues(temp); dt.rows.add(temp); } } } homecoming dt; } }

problem input array longer number of col in table. table has 4 columns problem? rows.add should adding row 4 elements same input.

you can utilize load method of datatable

dt.load(rdr);

whole code be

internal datatable connectedselect(string tablename, string[] cols, string condition) { datatable dt = new datatable(); using (sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["myconnectionstring"].connectionstring)) { con.open(); using (sqlcommand cmd = new sqlcommand(generateselectstatment(tablename, cols, condition),con)) { sqldatareader rdr = cmd.executereader(); if(rdr.hasrows) dt.load(rdr); } } homecoming dt; } }

c# ado.net

No comments:

Post a Comment