将IDataReader转换为DbDataReader

时间:2013-02-05 19:08:16

标签: asp.net ado.net

我在我的项目中使用SqlDataReader从数据库读取数据以将其发送到Web服务。我有这种方法来读取公司数据companyIDcompanyName我有一个转换问题:

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();
                }

            }

问题:

Error   1   Cannot implicitly convert type 'System.Data.IDataReader' to 'System.Data.Common.DbDataReader'. 
An explicit conversion exists (are you missing a cast?) 

1 个答案:

答案 0 :(得分:0)

更改GetCompanies()方法(首选)或DAL ExecuteReader()方法的返回类型。