Microsoft KB Archive/307512

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

Article ID: 307512

Article Last Modified on 12/3/2007

-

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

-



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.



Using Visual Basic .NET
 Open Microsoft Visual Studio .NET, and create a new Visual Basic Console Application project.  Add the following code above Module Module1: Imports System.Data.SqlClient   Add the following code to the Sub Main procedure: Dim cnNwind As New SqlConnection(&quot;Data Source=ServerName;user id=username;&quot; & _                                &quot;Password=password;Initial Catalog=Northwind;&quot;) Dim cmd As New SqlCommand(&quot;Select * from Customers&quot;, 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  Modify the connection string as appropriate for your Microsoft SQL Server. 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.</li></ol>

Using Visual C# .NET
<ol> Start Visual Studio .NET, and create a new Visual C# .NET Console Application project.</li>  Add the following code to Class1 before the ConsoleApplication1 namespace: using System.Data; using System.Data.SqlClient; </li>  Add the following code to the static void Main(string[] args) procedure: SqlConnection cnNwind = new SqlConnection(&quot;Data Source=servername;user id=username;                                         Password=password;Initial Catalog=Northwind;&quot;); SqlCommand cmd = new SqlCommand(&quot;Select * from Customers&quot;, 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; </li> Modify the connection string as appropriate for your SQL Server computer.</li> 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.</li></ol>

Using Visual C++ .NET
<ol> Start Visual Studio .NET, and create a Visual C++ .NET Managed Application project.</li>  Add the following code before the int _tmain(void) procedure:
 * 1) using <System.dll>
 * 2) using <System.Data.dll>

using namespace System; using namespace System::Data; using namespace System::Data::SqlClient ; </li>  Add the following code to the int -tmain(void) procedure: SqlConnection *cnNwind = new SqlConnection(&quot;Data Source=servername;user id=username;                                          Password=password;Initial Catalog=Northwind;&quot;); SqlCommand *cmd = new SqlCommand(&quot;Select * from Customers&quot;, 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; </li> Modify the connection string as appropriate for your SQL Server computer.</li> 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.</li></ol>

Using Visual J# .NET
<ol> Open Microsoft Visual Studio .NET, and create a new Visual J# Console Application project.</li>  Add the following code to class1: import System.Data.*; import System.Data.SqlClient.*; </li>  Add the following code to main: SqlConnection cnNwind = new SqlConnection(&quot;Data Source=servername;user id=username;Password=password;Initial Catalog=Northwind;&quot;); SqlCommand cmd = new SqlCommand(&quot;Select * from Customers&quot;, 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; </li> Modify the connection string as appropriate for your SQL Server computer.</li> 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.</li></ol>

Additional query words: kbreadme

Keywords: kbtshoot kbbug kbsqlclient kbsystemdata kbreadme kbpending kbvs2002sp1sweep KB307512

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.