The enumerated DataReader returns incomplete information for batch queries that have different schemas (308625)



The information in this article applies to:

  • Microsoft ADO.Net 2.0
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft .NET Framework

This article was previously published under Q308625

SYMPTOMS

If you create the DBEnumerator object on batch queries with different schemas, DBEnumerator returns incorrect or incomplete information. For example, if you run a batch query that contains two SELECT statements that return different schemas, the second statement may not return the entire schema.

CAUSE

This problem occurs because, by design, you can only use the DBEnumerator object with one result set.

RESOLUTION

To work around this behavior, obtain a DBEnumerator for each result set, and then call the NextResult method of a .NET data provider's DataReader object.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
  3. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
  4. Place a Button control on Form1. Button1 is added by default.
  5. Use the Imports statement on the System, System.Data, System.Data.Common, System.Collections and System.Data.SqlClient namespaces so that you are not required to qualify declarations in those namespaces later in your code.
    Imports System
    Imports System.Data
    Imports System.Data.Common
    Imports System.Collections
    Imports System.Data.SqlClient
    					
  6. In the Code window, add the following code after the "Windows Form Designer generated code" region:
    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
    
       Dim cn As New SqlConnection( _
           "server=servername;uid=sa;pwd=;database=Northwind")
       cn.Open()
       Dim cm As New System.Data.SqlClient.SqlCommand( _
           "SELECT FirstName FROM Employees WHERE EmployeeId = 1;" & _
           "SELECT FirstName, LastName FROM Employees WHERE " & _
           "EmployeeId = 2", cn)
    
       Dim dr As System.Data.SqlClient.SqlDataReader
       dr = cm.ExecuteReader
    
       Dim rEnum As System.Data.Common.DbEnumerator = _
          CType(CType(dr, System.Collections.IEnumerable).GetEnumerator(), _
          DbEnumerator)
    
       Debug.WriteLine("rEnum.MoveNext = " & rEnum.MoveNext())
    
       Dim record1 As DbDataRecord = (CType(rEnum.Current, DbDataRecord))
       Debug.WriteLine(record1.FieldCount)
       Debug.WriteLine(" The number of fields in this dataRecord " & _
           record1.FieldCount)
       Debug.WriteLine("The value of the first field is " & _
           record1.GetValue(0))
    
       dr.NextResult()
    
       rEnum.MoveNext()
    
       Dim record2 As DbDataRecord = (CType(rEnum.Current, DbDataRecord))
    
       ' Note that the DataRecord only displays a field count of 1,
       ' which is incorrect
       Debug.WriteLine("The number of fields in this DataRecord " & _
           record2.FieldCount)
       Debug.WriteLine("The value of the first field is = " & _
           record2.GetValue(0))
    
       dr.Close()
       cn.Close()
    End Sub
    					
  7. Modify the connection string as appropriate for your environment.
  8. Save your project. On the Debug menu, click Start to run your project.
  9. Click Button1. Press the CTRL+ALT+O key combination to view the Output window. Notice that the number of fields in the second DataRecord (record2) is 1, which is incorrect, and the LastName field is missing.
  10. To work around this behavior, add LastName to the SELECT statement to change the schema of the first query so that it matches the second query. Notice that the number of fields is correct.

Modification Type:MinorLast Reviewed:3/10/2006
Keywords:kbprb kbreadme kbSqlClient kbSystemData KB308625