Microsoft Knowledge Base Email Alertz

KBAlertz.com: (307512) - The GetSchemaTable method of the SqlDataReader object returns columns, the BaseServerName and BaseCatalogName properties. However, SqlDataReader.GetSchemaTable should only return BaseCatalogName.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 307512 - Last Review: December 3, 2007 - Revision: 1.17

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

This article was previously published under Q307512

On This Page

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.

APPLIES TO
  • Microsoft ADO.NET 2.0
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual J# .NET 2003 Standard Edition
  • Microsoft .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
Keywords: 
kbtshoot kbvs2002sp1sweep kbbug kbpending kbreadme kbsqlclient kbsystemdata KB307512
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please