Gravity
Gravity

Reputation: 229

Returns DbDataReader when SqlDataCommand.ExecuteReader()

I am writing a function to read data from sql database, the function is as follows,

public override Dictionary<string, DbDataReader> GetData()
{
    using (_connection)
    {
        Dictionary<string, DbDataReader> dataDictionary = new Dictionary<string, DbDataReader>();
        _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();

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

But I get a complier error, stating:

Cannot convert source type 'system.data.common.dbdatareader' to target type 'system.data.sqlclient.sqldatareader '

But I check the MSDN: https://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.110).aspx

Where it says that the return type of ExecuteReader() is 'system.data.sqlclient.sqldatareader'

Can anybody help me? Thank you!

Upvotes: 0

Views: 548

Answers (2)

Stephen Brickner
Stephen Brickner

Reputation: 2602

Your method is looking for a return type of Dictionary<string, DbDataReader> but you are returning Dictionary<string, SqlDataReader>.

public override Dictionary<string, DbDataReader> GetData()
{
    using (_connection)
    {
         Dictionary<string, DbDataReader> dataDictionary = new Dictionary<string, DbDataReader>();
         xmlDoc.Load("Queries.xml");
         XPathNavigator navigator = _xmlDoc.CreateNavigator();
         XPathNodeIterator iterator = navigator.Select("//query");
         while (iterator.MoveNext())
         {
             DbCommand _command = db.GetSqlStringCommand(iterator.Current.ToString());
             IDataReader dataReader = db.ExecuteReader(_command);

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

Upvotes: 1

Kalpesh Satasiya
Kalpesh Satasiya

Reputation: 76

Update your code replace DbDataReader to IDataReader..

public Dictionary<string, IDataReader> GetData()
    {
        using (_connection)
        {
            Dictionary<string, IDataReader> dataDictionary = new Dictionary<string, IDataReader>();
            _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();
                IDataReader reader = _command.ExecuteReader();

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

    }

Upvotes: 1

Related Questions