Question

Okay, my code is currently:

    public MySqlDataReader CreateQuery(string queryString, string connectionString )
    {

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            using (MySqlCommand command = new MySqlCommand(queryString, connection))
            {
                command.Connection.Open();
                command.ExecuteNonQuery();

                MySqlDataReader reader = command.ExecuteReader();
                return reader;
            }
        }
    }

In another function, I've got:

using(MySqlDataReader readers = _connection.CreateQuery(query, connectString))

Currently, in this form, when I return reader into a readers, there is no value. I've stepped through, and verified that at the point of the return, reader had the correct information in it. Yet readers has no values. I'm probably missing something completely silly. But any and all help in this will be appreciated. Thanks!

Was it helpful?

Solution

With the using command, your code is actually going to dispose of the connection and the command before control is returned to the caller. Essentially your connection object and your command object created in the using statements are disposed before the calling code is able to use it so its no longer usable.

You probably want to do something with the results of the query rather than the reader itself. Perhaps load it into a table? Something like:

public DataTable CreateQuery(string queryString, string connectionString )
    {
        DataTable results =  new DataTable("Results");
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            using (MySqlCommand command = new MySqlCommand(queryString, connection))
            {
                command.Connection.Open();
                command.ExecuteNonQuery();

                using (MySqlDataReader reader = command.ExecuteReader())
                    results.Load(reader);
            }
        }
       return results;
    }

OTHER TIPS

Instead of returning the Datareader or copy all records into an in-memory DataTable that you return as a whole, you could return an IEnumerable<IDataRecord> which is the basic interface a DataReader record implements.

So this should work since the yield causes the Connection to keep alive:

public IEnumerable<IDataRecord> CreateQuery(string queryString, string connectionString)
{
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
        using (MySqlCommand command = new MySqlCommand(queryString, connection))
        {
            command.Connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    yield return (IDataRecord)reader;
                }
            }
         }
    }
}

If you want for example take only the first 5 records:

var records = CreateQuery("SELECT * FROM ais.country;", Properties.Settings.Default.MySQL).Take(5);
foreach (IDataRecord rec in records)
{
    String country = rec.GetString(rec.GetOrdinal("Name"));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top