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
- 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("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()
- 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.
Using Visual C# .NET
- Start Visual Studio .NET, and create a new Visual C# .NET
Console Application project.
- Add the following code to Class1 before the ConsoleApplication1 namespace:
using System.Data;
using System.Data.SqlClient;
- 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();
- Modify the connection string as appropriate for your SQL
Server computer.
- 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
- Start Visual Studio .NET, and create a Visual C++ .NET
Managed Application project.
- 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 ;
- 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;
- Modify the connection string as appropriate for your SQL
Server computer.
- 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
- Open Microsoft Visual Studio .NET, and create a new Visual
J# Console Application project.
- Add the following code to class1:
import System.Data.*;
import System.Data.SqlClient.*;
- 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();
- Modify the connection string as appropriate for your SQL
Server computer.
- 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
| 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