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.

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.