I need to determine the structure of a result set returned by ExecuteReader. I am using the following approach:
public List<NameAndType> ResultSetStructure(DataTable columns)
{
var ret = new List<NameAndType>();
foreach (DataRow column in columns.Rows)
{
ret.Add(new NameAndType { Name = column[NameIndex].ToString(),
Type = column[TypeIndex].ToString()
});
}
return ret;
}
(snip)
using (SqlDataReader dr = command.ExecuteReader())
{
var rawColumns = dr.GetSchemaTable();
var columns = ResultSetStructure(rawColumns);
This gives me column names and types, but I would also like to know if the column is nullable, so that I know which of the following options to choose:
decimal density = dr.GetDecimal(0);
decimal? density = dr.IsDBNull(0) ? (decimal?)null : dr.GetDecimal(0);
Can I accomplish that? TIA.
Edit: I just found what I need:
column[13].ToString()