Generic function to handle DataType Conversion

Posted by Tommo1977 on Stack Overflow See other posts from Stack Overflow or by Tommo1977
Published on 2013-11-01T08:57:00Z Indexed on 2013/11/01 15:54 UTC
Read the original article Hit count: 266

Filed under:
|
|

I have the following function in C#, that I want to pass a MySQL DataReader value/object.

If the value of the object passed is a DBNull I need to handle it and return a Null value of that data type e.g null int.

Otherwise I want to convert the returned value like Convert.ToInt32(value) or similar.

public static T ConvertFromDB<T>(object value)
{
     return value == DBNull.Value ? default(T) : (T)value;
}

Can anyone offer any help on this ?

© Stack Overflow or respective owner

Related posts about c#

Related posts about mysql