当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - casting IDataReader to DbDataReader

问题描述:

I'm using SqlDataReader on my project to read data From DB to send it to Web service.I had this method to read company data companyID and companyName I have a casting problem:

public static DbDataReader GetCompanies()

{

DbDataReader dr = null;

DbCommand comm = GenericDataAccess.CreateCommand();

comm.CommandType = CommandType.StoredProcedure;

try

{

dr = GenericDataAccess.ExecuteReader(comm);

dr.Read();

}

catch (Exception ex)

{

throw ex;

}

return dr;

}

//ExecuteReader DAL

public static IDataReader ExecuteReader(DbCommand command)

{

try

{

command.Connection.Open();

return command.ExecuteReader(CommandBehavior.CloseConnection);

}

catch (Exception ex)

{

Utilities.LogError(ex);

throw;

}

finally

{

command.Connection.Close();

}

}

Problem:

Error 1 Cannot implicitly convert type 'System.Data.IDataReader' to 'System.Data.Common.DbDataReader'.

An explicit conversion exists (are you missing a cast?)

网友答案:

Change the return-type of either your GetCompanies() method (preferred) or of your DAL ExecuteReader() method.

分享给朋友:
您可能感兴趣的文章:
随机阅读: