c# - Returning datatable using entity framework -
i using entity framework. there 1 particular situation in application have use stored procedure. since there lot of sql statements written in sp, don't want re-write in c# code. need result in form of datatable. have written little bit of code stuck @ 1 point. can complete code below?
using (dbcontext.database.connection) { dbcontext.database.connection.open(); dbcommand cmditems= dbcontext.database.connection.createcommand(); cmditems.commandtext = "getavailableitems"; cmditems.commandtype = commandtype.storedprocedure; cmditems.parameters.add(new sqlparameter("jobcardid", 100525)); //need write code below populate datatable. }
thanks lot guys. solved it. here solution:
datatable dt = new datatable(); var context = new databasecontext(); var conn = context.database.connection; var connectionstate = conn.state; try { using (context) { if (connectionstate != connectionstate.open) conn.open(); using (var cmd = conn.createcommand()) { cmd.commandtext = "getavailableitems"; cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add(new sqlparameter("jobcardid", 100525)); using (var reader = cmd.executereader()) { dt.load(reader); } } } } catch (exception ex) { throw ex; } { if (connectionstate != connectionstate.open) conn.close(); } return dt;
Comments
Post a Comment