c# - Cannot implicitly convert type System.Data.SqlClient.SqlDataReader to Microsoft.ReportingServices.DataProcessing.IDataReader -
i'm receiving following error:
cannot implicitly convert type 'system.data.sqlclient.sqldatareader' 'microsoft.reportingservices.dataprocessing.idatareader'. explicit conversion exists (are missing cast?)
public idatareader executereader(commandbehavior behavior) { string query = "select * sales.store"; sqlconnection readerconn = new sqlconnection("data source=localhost;initial catalog=adventureworks2000;integrated security=sspi"); sqlcommand readercmd = new sqlcommand(query); try { readerconn.open(); readercmd = readerconn.createcommand(); readercmd.commandtext = query; readercmd.commandtype = system.data.commandtype.text; sqldatareader testreader = readercmd.executereader(); return testreader; //getting error @ line readerconn.close(); } catch (exception e) { throw new exception(e.message); } }
after modifying code per radin gospodinov suggestion, not getting error unable data dataset.here modified code
public class mysqldatareader:microsoft.reportingservices.dataprocessing.idatareader { private system.data.idatareader sourcedatareader; private system.data.datatable dt; private system.data.dataset ds; public mysqldatareader(system.data.idatareader datareader) { this.sourcedatareader = datareader; } public mysqldatareader(system.data.datatable dt) { // todo: complete member initialization this.dt = dt; } public mysqldatareader(system.data.dataset ds) { // todo: complete member initialization this.ds = ds; } public idatareader executereader(commandbehavior behavior) { string query = "select * sales.store"; sqlconnection readerconn = new sqlconnection("data source=ie11dt4rjc6q1;initial catalog=adventureworks2000;integrated security=sspi"); sqlcommand readercmd = new sqlcommand(query); try { readerconn.open(); readercmd = readerconn.createcommand(); readercmd.commandtext = query; readercmd.commandtype = system.data.commandtype.text; sqldatareader testreader = readercmd.executereader(); //testreader.close(); readerconn.close(); //readerconn.open(); sqldataadapter adapter = new sqldataadapter(query,readerconn); adapter.selectcommand = readercmd; system.data.dataset ds = new system.data.dataset(); adapter.fill(ds, "result"); system.data.datatable dt = new system.data.datatable(); adapter.fill(dt); readerconn.close(); return new mysqldatareader(ds); } catch (exception e) { throw new exception(e.message); } }
sqldatareader implements system.data.idatareader, if want cast idatarader should use public system.data.idatareader executereader(commandbehavior behavior)
close connection in block:
try { readerconn.open() } catch{...} { readerconn.close(); }
if want use microsoft.reportingservices.dataprocessing.idatareader should create class implements , pass sqldatareader it:
public class mysqldatareaderwrapper: microsoft.reportingservices.dataprocessing.idatareader { private system.data.idatareader sourcedatareader; public mysqldatareaderwrapper(system.data.idatareader datareader) { this.sourcedatareader = datareader. } // implementation of methods of microsoft.reportingservices.dataprocessing.idatareader }
then in method use return new mysqldatareaderwrapper(testdatareader);
Comments
Post a Comment