0

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!

2 Answers 2

1

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

    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

Even I changed the signature, this error still exists. And the reason why I use this signature is because I want to override a base function, so that I can do the same executions with sql and oracle databases. Like stated in : stackoverflow.com/questions/33341435/…
I didn't notice you were overriding a method. So basically then you can't add the SqlDataReader to dictionary like you are in the while loop. It would have to be a DbDataReader.
I made an update to the code. That should return you the proper type.
Thank you! But what is the type of db?
It's of type Database db. It's like your SqlConnection class. You may just be able to use SqlConnection and SqlCommand still and just use the IDataReader to read the sql command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.