Bug #74958 MySqlDataAdapter passes data to DataTable by reference rather than by value
Submitted: 21 Nov 2014 11:13 Modified: 21 May 2020 18:38
Reporter: Piers Karsenbarg Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.9.5.0 OS:Windows
Assigned to: CPU Architecture:Any

[21 Nov 2014 11:13] Piers Karsenbarg
Description:
When using MySqlDataAdapter to fill a DataTable (or DataSet), the data only sits in the DataTable for as long as the MySqlDataAdapter is in scope.

How to repeat:
DataTableReader reader;
            using (var connection = new MySqlConnection(_connectionString))
            {
                using (var command = new MySqlCommand())
                {
                    command.CommandType = commandType;
                    command.CommandText = sql;
                    command.Connection = connection;
                    if (parameters != null)
                    {
                        command.Parameters.AddRange(parameters);
                    }
                    using (var adapter = new MySqlDataAdapter())
                    {
                        adapter.SelectCommand = command;
                        using (var dataTable = new DataTable())
                        {
                            adapter.Fill(dataTable);
                            reader = dataTable.Copy().CreateDataReader();
                            // at this point reader contains rows of data
                        }
                    }
                }
            }
            while (reader.Read())
            {
                // at this point the reader is empty
            }

Suggested fix:
When MySqlDataAdapter.Fill() is called, pass the value by value rather than reference.
[21 Nov 2014 12:00] Piers Karsenbarg
After doing a little bit more digging, it seems that the MySqlDataAdapter.Fill() method comes straight from DbDataAdapter that SqlDataAdapter uses, so how come it's passing the data by reference rather than value?
[21 Jan 2015 5:35] Chiranjeevi Battula
Hello Piers Karsenbarg,

Thank you for the bug report.
Verified this behavior on Visual Studio 2013 (C#.Net) with  MySQL Connector/Net 6.9.5.

Thanks,
Chiranjeevi.
[21 May 2020 18:38] Daniel Valdez
Posted by developer:
 
Verified using latest Connector/NET version, 8.0.20, and this behavior is no longer occurring.