can i get a list of field names in a SqlDataReader ?

G

Guest

Hi guys

Ok, here's the situation. I'm building a reporting system for my
application, that basically creates a CSV file based on a load of data in my
database.

I'm trying to make the application as "extensible" as possible, by not
hardcoding anything. So I'm thinking you can select a report type, and that
runs a peice of code that runs a stored proceedure and puts the results into
a CSV.

I suppose the first question is "is there an easy to way to do that that I
haven't found yet?".

My idea is to simply go through the data reader and dump everytihng to CSV,
the problem is.. I can't do that cos I won't know how many fields there are.
Can I find that out from the DataReader, and can I grab the field names from
the DataReader so that I can put these at the top of my CSV?!

Any help/ideas appreciated!

Cheers



Dan
 
K

Kevin Spencer

Hi Dan,
My idea is to simply go through the data reader and dump everytihng to
CSV,
the problem is.. I can't do that cos I won't know how many fields there
are.
Can I find that out from the DataReader, and can I grab the field names
from
the DataReader so that I can put these at the top of my CSV?!

Yes, you can, and yes there is.

A DataReader has the following properties and methods for retrieving column
information:

FieldCount - returns the number of columns in the result set.
GetFieldType(int) - returns the Type of the column.
GetDataTypeName(int) - returns the Name of the back-end database column
type.
GetName(int) - returns the Column Name.

So you would start by getting the FieldCount, and create a loop to loop
through the Fields (columns) in the DataReader, getting whatever information
you need about each one.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
I'd rather be a hammer than a nail.
 
G

Guest

Wow - that's very helpful. Just written the whole function now with that
little bit of advice.

Thanks Kevin - I owe you some beer!
 
Joined
Dec 31, 2007
Messages
1
Reaction score
0
=?Utf-8?B?ZGhucml2ZXJzaWRl?= said:
Hi guys

Ok, here's the situation. I'm building a reporting system for my
application, that basically creates a CSV file based on a load of data in my
database.

I'm trying to make the application as "extensible" as possible, by not
hardcoding anything. So I'm thinking you can select a report type, and that
runs a peice of code that runs a stored proceedure and puts the results into
a CSV.

I suppose the first question is "is there an easy to way to do that that I
haven't found yet?".
Any help/ideas appreciated!

Cheers


Dan

I think you are looking for the GetSchemaTable method. Check it out here.
:captain:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top