覆盖方法(返回类型DBDataReader)并返回不同的类型(SQLDBDataReader)

时间:2015-10-26 08:33:25

标签: c# database

我目前正在实施一个可以从不同数据库读取数据的小软件。以下是代码:

interface Fetch
{
    Dictionary<string, DbDataReader> GetData();
}

abstract class Conn : Fetch
{ 
    abstract public void Connect();
    abstract public Dictionary<string, SqlDataReader> GetData();
}

class SqlConn : Conn 
{
    public override void Connect() 
    {
        _connection = new SqlConnection(_connectionString);
        try
        {
            _connection.Open();
        }
        catch (SqlException dbe)
        {
            throw dbe;
        }
    }
    public override Dictionary<string, SqlDataReader> GetData()
    {
        using (_connection)
        {
            Dictionary<string, SqlDataReader> dataDictionary = new Dictionary<string, SqlDataReader>();
            _xmlDoc.Load("Queries.xml");
            XPathNavigator navigator = _xmlDoc.CreateNavigator();
            XPathNodeIterator iterator = navigator.Select("//query");
            while (iterator.MoveNext())
            {
                _command = new SqlCommand(iterator.Current.ToString());
                _command.Connection = _connection;
                _command.CommandText = iterator.Current.ToString();
                SqlDataReader reader = _command.ExecuteReader() as SqlDataReader;

                dataDictionary.Add(iterator.Current.GetAttribute("name", ""), reader);
            }
            return dataDictionary;
        }
    }
}

class OraConn : Conn 
{
    public override void Connect()
    {
        _connection = new OracleConnection(_connectionString);
    }
    public override Dictionary<string, OracleDataReader> GetData()
    {
       using (_connection)
        {
            Dictionary<string, OracleDataReader> dataDictionary = new Dictionary<string, OracleDataReader>();
            _xmlDoc.Load("Queries.xml");
            XPathNavigator navigator = _xmlDoc.CreateNavigator();
            XPathNodeIterator iterator = navigator.Select("//query");
            while (iterator.MoveNext())
            {
                _command = new OracleCommand(iterator.Current.ToString());
                _command.Connection = _connection;
                _command.CommandText = iterator.Current.ToString();
                OracleDataReader reader = _command.ExecuteReader() as OracleDataReader;

                dataDictionary.Add(iterator.Current.GetAttribute("name", ""), reader);
            }

            return dataDictionary; 
        }
    }
}

但我的问题是派生类中的返回类型,SQLDataReader和OraDataReader。 这会导致编译器错误,表明

  

'错误2'DashBoard.Connection.OraConn.GetData()':返回类型必须是'System.Collections.Generic.Dictionary'才能匹配重写成员'DashBoard.Connection.Conn.GetData()   ”。

我该如何解决这个问题?或者还有其他方法来实现这个功能吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

方法签名必须保持不变,但只要您的子类继承自DbDataReader,您就可以返回其他DbDataReader类型,例如SqlDataReader,它将进行编译。< / p>

例如:

abstract class DbDataReader
{
    // ...
}

class SqlDataReader : DbDataReader
{
}

// ...

class SqlConn : Conn 
{
    public override Dictionary<string, DbDataReader> GetData()
    {
       return new Dictionary<string, DbDataReader>
       {
            { "Key", new SqlDataReader() } 
       }
    }
}

答案 1 :(得分:1)

您可以尝试这种方式:

public interface IValue<T> { T GetValue(); }
public class SomeClass : IValue<DbDataReader>, IValue<SqlDataReader>
{
    DbDataReader IValue<DbDataReader>.GetValue() { return objDbDataReader; }
    SqlDataReader IValue<SqlDataReader>.GetValue() { return objSqlDataReader; }
}