Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 9th, 2009, 08:09 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default Get column names and values from data reader filled from excel file

Hi,

I am struggling to find a way to read the column names and values from a Data Reader that I populated from an excel file and would appreciate very much is someone could help me.

As you can see, I have produced a very simple excel file. The first row contains the column headers and the other rows contain the values.

Company Audit JARScan
test1 Y
test2 Y

Once I read the excel file into a data reader, I thought I would have access to its header information as well.

I can easily get the values from the cells by doing this:

dr("Audit")

But what about getting the headers and values dynamically?

I just need a way to dynamically create a name / value pair. It could even be a XML file like this:

<Permissions>
<perm type="Company" value="test1"/>
<perm type="Audit" value=""/>
<perm type="JARScan" value"Y"/>
<Permissions>

So the type attribute would contain the name of the column/heading and the value would contain the value of the cell for that row.

I would apprediate very much if could shed some light.

I have used the code below but to no avail. When it comes to read the
column names it does not work

dt.Columns[i].ColumnName;
string cn = dr.GetSchemaTable().Columns[i].ColumnName;
string val = dr.GetSchemaTable().Columns[i].DefaultValue.ToString();

THE CODE
===========


using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;

using (DbCommand command = connection.CreateCommand())
{
// sheet$ comes from the name of the worksheet
string sheet = ddlSheets.SelectedItem.Text;
command.CommandText = "SELECT * FROM [" + sheet + "]";

connection.Open();

using (DbDataReader dr = command.ExecuteReader())
{

DataTable columns;

//columns = connection.GetSchema("Columns");

DataTable dt = dr.GetSchemaTable();
for (int i=0;i<dt.Columns.Count;i++)
{
string cl = dt.Columns[i].ColumnName;
}



while (dr.Read())
{

for (int i = 0; i < dr.GetSchemaTable().Columns.Count; i++)
{
string cn = dr.GetSchemaTable().Columns[i].ColumnName;
string val = dr.GetSchemaTable().Columns[i].DefaultValue.ToString();


}

}
}
}
}
}


Cheers

C





Similar Threads
Thread Thread Starter Forum Replies Last Post
Change embeded pictures in excel to file names exceldude Excel VBA 2 October 30th, 2012 04:46 AM
Excel File Reader Problem umair.hasan ASP.NET 1.0 and 1.1 Professional 0 August 17th, 2007 11:12 AM
Column names! Help! dirtdog22 Access VBA 6 June 1st, 2007 06:37 AM
export data grid hyperlink column to excel phyzie VS.NET 2002/2003 0 April 16th, 2007 01:45 AM
Query for Column Names and Datatypes rstelma SQL Server 2000 3 August 23rd, 2005 02:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.