Using c# how do I print all columns in a datareader.
A:
Start by looking up decent DataReader tutorials from Google.
This is an example of one: CSharp-Station
espais
2010-04-29 15:46:53
+2
A:
This method will return an enumerable list of column names when passed a datareader:
static List<string> GetDataReaderColumnNames(IDataReader rdr)
{
var columnNames = new List<string>();
for (int i = 0; i < rdr.FieldCount; i++)
columnNames.Add(rdr.GetName(i));
return columnNames;
}
RedFilter
2010-04-29 15:47:46
+1
A:
for (int j = 0; j < x.VisibleFieldCount; j++)
Console.WriteLine(x.GetName(j));
Tejs
2010-04-29 15:48:22
+2
A:
To add some value to the answers, I included a possible extension method to return the column names for a given DataReader
.
public static IEnumerable<string> GetColumnNames(this IDataReader reader)
{
for(int i=0; i<reader.FieldCount; i++)
yield return reader.GetName(i);
}
Justin Niessner
2010-04-29 15:51:01
I love extension methods,i feel like injecting into and exposing that behaviour.. :)
Srinivas Reddy Thatiparthy
2010-04-29 16:19:54