문제

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