Вопрос

If I create a new OleDbReader as follows:

OleDbCommand command = new OleDbCommand(query, ActiveConnection);
reader = command.ExecuteReader();
while(reader.Read())
{
   someList.Add((double)reader["columnHeader"]);
}

How can I ensure that I'm always returned a double from the column specified, if the data returned is typed as Int16/Int32/Int64 instead of Double? Do I have to create a handler for each possible type? I know Double.Parse exists but it only accepts strings. So, while I could use ToString() beforehand I feel that this is probably not the most straightforward way to typecast.

Similarly, for another column, I wish to ensure that any String/Double/Int16/Int32/Int64 values return as strings. Will ToString() handle these cases?

I'm fine with potential overflow errors, as I'll check for exceptions regardless.

Это было полезно?

Решение

You should call Convert.ToDouble(), which has a handler for each type.

By contrast, ToString() will work for any non-null value. (as long as it overrides ToString(); all numeric types do)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top