[Mono-bugs] [Bug 561667] New: Disposing a SqlCommand should dispose associated SqlDataReaders

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Dec 8 12:21:09 EST 2009


http://bugzilla.novell.com/show_bug.cgi?id=561667

http://bugzilla.novell.com/show_bug.cgi?id=561667#c0


           Summary: Disposing a SqlCommand should dispose associated
                    SqlDataReaders
    Classification: Mono
           Product: Mono: Class Libraries
           Version: SVN
          Platform: x86-64
        OS/Version: openSUSE 11.2
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: Sys.Data
        AssignedTo: bnc-blr-team-mono at forge.provo.novell.com
        ReportedBy: jpryor at novell.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---


Disposing a System.Data.SqlClient.SqlCommand should close any associated
SqlDataReaders.  Consider this code snippet:

        protected override DataTable GetColumns(DbConnection connection)
        {
            var t = new DataTable("Columns");
            using (var c = connection.CreateCommand())
            {
                c.CommandText = "SELECT * FROM [TABLE]";
                t.Load(c.ExecuteReader());
            }
            return t;
        }

Notice that we ensure that the SqlCommand is disposed.

However, if we subsequently call this same method a second time (or a method
that's identical save for the SQL, the SQL query is really irrelevant), then we
get:

System.InvalidOperationException: There is already an open DataReader
associated with this Connection which must be closed first.
  at System.Data.SqlClient.SqlCommand.ValidateCommand (System.String method,
Boolean async) [0x0010e] in
/home/jon/Development/mono-HEAD/mcs/class/System.Data/System.Data.SqlClient/SqlCommand.cs:776 
  at System.Data.SqlClient.SqlCommand.ExecuteReader (CommandBehavior behavior)
[0x00000] in
/home/jon/Development/mono-HEAD/mcs/class/System.Data/System.Data.SqlClient/SqlCommand.cs:585 
  at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader (CommandBehavior
behavior) [0x00000] in
/home/jon/Development/mono-HEAD/mcs/class/System.Data/System.Data.SqlClient/SqlCommand.cs:786 
  at System.Data.Common.DbCommand.ExecuteReader () [0x00000] in
/home/jon/Development/mono-HEAD/mcs/class/System.Data/System.Data.Common/DbCommand.cs:123 
  at (wrapper remoting-invoke-with-check)
System.Data.Common.DbCommand:ExecuteReader ()

The reason is because the SqlCommand.Dispose() method doesn't call
SqlCommand.CloseDataReader().  To allow such use the user needs to explicitly
dispose the SqlDataReader:

        protected override DataTable GetColumns(DbConnection connection)
        {
            var t = new DataTable("Columns");
            using (var c = connection.CreateCommand())
            {
                c.CommandText = "SELECT * FROM [TABLE]";
                using (var r = c.ExecuteReader())
                    t.Load(r);
            }
            return t;
        }

The point is, though, the original method works as expected under .NET (no
exception), while the second method is required under Mono.  Mono should
support the same source code as .NET.

-- 
Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.


More information about the mono-bugs mailing list