BUG: The GetSchemaTable method of the SqlDataReader object returns the wrong column name (307512)



The information in this article applies to:

  • Microsoft ADO.Net 2.0
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual J# .NET 2002
  • Microsoft .NET Framework
  • Microsoft Windows .NET Framework 1.1
  • Microsoft .NET Framework 2.0
  • Microsoft Visual Basic 2005
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual J# 2005 Express Edition

This article was previously published under Q307512

SYMPTOMS

The GetSchemaTable method of the SqlDataReader object returns columns, the BaseServerName and BaseCatalogName properties. However, SqlDataReader.GetSchemaTable should only return BaseCatalogName.

CAUSE

In addition to the columns that are described in the Microsoft .NET Framework Class Library Software Development Kit (SDK) documentation, SqlDataReader.GetSchemaTable returns a column named BaseServerName. BaseServerName is the name of the Microsoft SQL Server instance that SqlDataReader uses. BaseServerName appears before BaseCatalogName in the DataTable that describes the column metadata.

RESOLUTION

Because this problem does not occur with the OleDbDataReader object, you can use OleDbDataReader to obtain the SchemaTable information.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

Using Visual Basic .NET

  1. Open Microsoft Visual Studio .NET, and create a new Visual Basic Console Application project.
  2. Add the following code above Module Module1:
    Imports System.Data.SqlClient
    					
  3. Add the following code to the Sub Main() procedure:
    Dim cnNwind As New SqlConnection("Data Source=ServerName;user id=username;" & _
                                     "Password=password;Initial Catalog=Northwind;")
            Dim cmd As New SqlCommand("Select * from Customers", cnNwind)
            cnNwind.open()
    
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader
            Dim i As Integer
            For i = 0 To dr.GetSchemaTable.Columns.Count - 1
    
            System.Console.WriteLine(dr.GetSchemaTable.Columns(i).ColumnName)
    
            Next
            System.Console.Read()        
            dr.Close()
            cnNwind.Close()
    					
  4. Modify the connection string as appropriate for your Microsoft SQL Server.
  5. Press the F5 key to compile and run the project. Notice that all of the columns of the SchemaTable appear in the Console window. In addition, notice that BaseServerName also appears before BaseCatalogName.

Using Visual C# .NET

  1. Start Visual Studio .NET, and create a new Visual C# .NET Console Application project.
  2. Add the following code to Class1 before the ConsoleApplication1 namespace:
    using System.Data;
    using System.Data.SqlClient;
    					
  3. Add the following code to the static void Main(string[] args) procedure:
    SqlConnection cnNwind = new SqlConnection("Data Source=servername;user id=username;
                                              Password=password;Initial Catalog=Northwind;");
            SqlCommand cmd = new SqlCommand("Select * from Customers", cnNwind);
    	
            cnNwind.Open();
    
            SqlDataReader dr ;
    
    			dr = cmd.ExecuteReader();
    			DataTable dt = dr.GetSchemaTable();
    				for (int i=0;i<dt.Columns.Count;i++)
    				{
    					System.Console.WriteLine(dt.Columns[i]);
    				}
    				System.Console.Read();
    
                                    dr.Close();
                                    cnNwind.Close();
    			
    					
  4. Modify the connection string as appropriate for your SQL Server computer.
  5. Press the F5 key to compile and run the project. Notice that all of the columns of the SchemaTable appear in the Console window. In addition, notice that BaseServerName also appears before BaseCatalogName.

Using Visual C++ .NET

  1. Start Visual Studio .NET, and create a Visual C++ .NET Managed Application project.
  2. Add the following code before the int _tmain(void) procedure:
    #using <System.dll>
    #using <System.Data.dll>
    
    using namespace System;
    using namespace System::Data;
    using namespace System::Data::SqlClient ;
    					
  3. Add the following code to the int -tmain(void) procedure:
    SqlConnection *cnNwind = new SqlConnection("Data Source=servername;user id=username;
                                               Password=password;Initial Catalog=Northwind;");
        SqlCommand *cmd = new SqlCommand("Select * from Customers", cnNwind);
    		 cnNwind->Open();
    
            SqlDataReader *dr ;
    		
    			dr = cmd->ExecuteReader();
    			DataTable *dt = dr->GetSchemaTable();
    			int i;
    			for (i=0;i<dt->Columns->Count;i++)
    				{
                                     Console::WriteLine(dt->Columns->Item [i]);
    				}
    				System::Console::Read();
    				dr->Close();
    				cnNwind->Close();
    			
                                    return 0;
    					
  4. Modify the connection string as appropriate for your SQL Server computer.
  5. Press the F5 key to compile and run the project. Notice that all of the columns of the SchemaTable appear in the Console window. In addition, notice that BaseServerName also appears before BaseCatalogName.

Using Visual J# .NET

  1. Open Microsoft Visual Studio .NET, and create a new Visual J# Console Application project.
  2. Add the following code to class1:
    import System.Data.*;
    import System.Data.SqlClient.*;
    					
  3. Add the following code to main:
    	SqlConnection cnNwind = new SqlConnection("Data Source=servername;user id=username;Password=password;Initial Catalog=Northwind;");
    		SqlCommand cmd = new SqlCommand("Select * from Customers", cnNwind);
    	
    		cnNwind.Open();
    
    		SqlDataReader dr ;
    
    		dr = cmd.ExecuteReader();
    		DataTable dt = dr.GetSchemaTable();
    		for (int i = 0; i < dt.get_Columns().get_Count(); i++)
    		{
    			DataColumnCollection dcc = dt.get_Columns();
    			System.Console.WriteLine(dcc.get_Item(i));
    		}
    		System.Console.Read();
    	
    		dr.Close();
    		cnNwind.Close();
    					
  4. Modify the connection string as appropriate for your SQL Server computer.
  5. Press the F5 key to compile and run the project. Notice that all of the columns of the SchemaTable appear in the Console window. In addition, notice that BaseServerName also appears before BaseCatalogName.

Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbtshoot kbvs2002sp1sweep kbbug kbpending kbreadme kbSqlClient kbSystemData KB307512 kbAudDeveloper