0

I'm trying to retrieve user selected data from a database. The user can select up to five fields to show in a report and the fields are a mixture of strings, dates and numbers. I cannot convert the data to strings in the SQL code due to other reports being run. Since I don't know the datatype, I can't do datareader.GetSqlString(column), and since I don't know the name of the column the user may have selected, I can't do datareader.GetString(datareader.GetOrdinal(columnName)). Any ideas on how to retrieve the values from the database without knowing either the datatype or the column name?

Thanks in advance.

KMBonin
  • 111
  • 4
  • 12

1 Answers1

1

You can use reader.GetSchemaTable() to get a table of the schema of the data reader or reader.GetType(n) or reader.GetProviderSpecificType(n) to get the datatype of a particular field.

You can then use that information to decide which method to call:

if (reader.GetType(n) == typeof(string))
{
    string value = reader.GetString(n);
}
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48