There is no need to worry about DataRow type conversion and null values (using extended methods to solve high-frequency problems), and no need to worry about datarow
When reading data using DataRow, data may be Null, but must be converted to other types of data such as int. Therefore, the following code is usually written:
If (dr [name]! = DBNull. Value & dr [name]! = Null)
{
If (! Int. TryParse (dr [name]. ToString (), out result ))
Throw new Exception ("Integer Conversion failed ");
}
And such code usually appears in many places.
For a relatively lazy programmer, such a repeat yourself is a very painful thing, so it is time to come up with a better method. The first thing that comes to mind here is the Extension Method.
A few simple codes. With this method, you can read data through the extension method when using DataRow.
model.Id = dr.SafeRead<Int32>("Id",0);
model.codeNum = dr.GetString("codeNum");
Simple but practical code, hope to help you in the project.
For more information, see the following two articles, reorganize the code, and finally sort out the class source code.
Use Extension Methods to make IDataReader more convenient
C # common extensions of the underlying SqlDataReader class
Public static class SystemDataExtension {# region DataReader extends public static T SafeRead <T> (this IDataReader reader, string fieldName, T defaultValue) {try {object obj = reader [fieldName]; if (obj = null | obj = System. DBNull. value) return defaultValue; return (T) Convert. changeType (obj, defaultValue. getType () ;}catch {return defaultValue ;}/// <summary> /// obtain string-type data /// </summary> /// <Param name = "dr"> </param> /// <param name = "name"> </param> /// <returns> </returns> public static string GetString (this IDataReader dr, string name) {if (dr [name]! = DBNull. Value & dr [name]! = Null) return dr [name]. toString (); return String. empty ;} /// <summary> /// obtain non-empty data // </summary> /// <param name = "con"> </param> // <param name = "name"> </param> // <returns> </returns> public static DateTime GetDateTime (this IDataReader dr, string name) {DateTime result = DateTime. now; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! DateTime. tryParse (dr [name]. toString (), out result) throw new Exception ("Date Format Data Conversion failed");} return result ;} /// <summary> /// obtain data of an empty date type /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static DateTime? GetNullDateTime (this IDataReader dr, string name) {DateTime? Result = null; DateTime time = DateTime. Now; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! DateTime. tryParse (dr [name]. toString (), out time) throw new Exception ("Date Format Data Conversion failed"); result = time;} return result ;} /// <summary> /// obtain guid-type data /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static Guid GetGuid (this IDataReader dr, string name) {Guid guid = Guid. empty; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (Guid. tryParse (dr [name]. toString (), out guid) throw new Exception ("guid type Data Conversion failed");} return guid ;} /// <summary> /// obtain the integer data /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static int GetInt32 (this IDataReader dr, string name) {int result = 0; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Int. tryParse (dr [name]. toString (), out result) throw new Exception ("Integer Conversion failed");} return result ;} /// <summary> /// obtain double-precision data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static double GetDouble (this IDataReader dr, string name) {double result = 0.00; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Double. tryParse (dr [name]. toString (), out result) throw new Exception ("double-precision type conversion failed");} return result ;} /// <summary> /// obtain single-precision data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static float GetSingle (this IDataReader dr, string name) {float result = 0.00f; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Float. tryParse (dr [name]. toString (), out result) throw new Exception ("single precision type conversion failed");} return result ;} /// <summary> /// obtain decimal data /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static decimal GetDecimal (this IDataReader dr, string name) {decimal result = 0.00 m; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Decimal. tryParse (dr [name]. toString (), out result) throw new Exception ("Decimal type conversion failed");} return result ;} /// <summary> /// obtain int16 type data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static Int16 GetInt16 (this IDataReader dr, string name) {short result = 0; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Short. tryParse (dr [name]. toString (), out result) throw new Exception ("Short Integer Conversion failed");} return result ;} /// <summary> /// obtain Byte data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static byte GetByte (this IDataReader dr, string name) {byte result = 0; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Byte. tryParse (dr [name]. toString (), out result) throw new Exception ("failed Byte type conversion");} return result ;} /// <summary> /// obtain the bool type data. If the value is 1, true is returned; /// </summary> /// <param name = "con"> </param> /// <param name = "name"> </param> /// <returns> </returns> public static bool GetBool (this IDataReader dr, string name) {if (dr [name]! = DBNull. Value & dr [name]! = Null) {return dr [name]. toString () = "1" | dr [name]. toString () = "yes" | dr [name]. toString (). toLower () = "true";} return false;} # endregion # region DataRow extends public static T SafeRead <T> (this DataRow dr, string fieldName, T defaultValue) {try {object obj = dr [fieldName]; if (obj = null | obj = System. DBNull. value) return defaultValue; return (T) Convert. changeType (obj, defaultValue. getTy Pe ();} catch {return defaultValue ;}} /// <summary> /// obtain string-type data /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static string GetString (this DataRow dr, string name) {if (dr [name]! = DBNull. Value & dr [name]! = Null) return dr [name]. toString (); return String. empty ;} /// <summary> /// obtain non-empty data // </summary> /// <param name = "con"> </param> // <param name = "name"> </param> // <returns> </returns> public static DateTime GetDateTime (this DataRow dr, string name) {DateTime result = DateTime. now; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! DateTime. tryParse (dr [name]. toString (), out result) throw new Exception ("Date Format Data Conversion failed");} return result ;} /// <summary> /// obtain data of an empty date type /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static DateTime? GetNullDateTime (this DataRow dr, string name) {DateTime? Result = null; DateTime time = DateTime. Now; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! DateTime. tryParse (dr [name]. toString (), out time) throw new Exception ("Date Format Data Conversion failed"); result = time;} return result ;} /// <summary> /// obtain guid-type data /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static Guid GetGuid (this DataRow dr, string name) {Guid guid = Guid. empty; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (Guid. tryParse (dr [name]. toString (), out guid) throw new Exception ("guid type Data Conversion failed");} return guid ;} /// <summary> /// obtain the integer data /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static int GetInt32 (this DataRow dr, string name) {int result = 0; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Int. tryParse (dr [name]. toString (), out result) throw new Exception ("Integer Conversion failed");} return result ;} /// <summary> /// obtain double-precision data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static double GetDouble (this DataRow dr, string name) {double result = 0.00; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Double. tryParse (dr [name]. toString (), out result) throw new Exception ("double-precision type conversion failed");} return result ;} /// <summary> /// obtain single-precision data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static float GetSingle (this DataRow dr, string name) {float result = 0.00f; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Float. tryParse (dr [name]. toString (), out result) throw new Exception ("single precision type conversion failed");} return result ;} /// <summary> /// obtain decimal data /// </summary> /// <param name = "dr"> </param> /// <param name = "name"> </param> // <returns> </returns> public static decimal GetDecimal (this DataRow dr, string name) {decimal result = 0.00 m; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Decimal. tryParse (dr [name]. toString (), out result) throw new Exception ("Decimal type conversion failed");} return result ;} /// <summary> /// obtain int16 type data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static Int16 GetInt16 (this DataRow dr, string name) {short result = 0; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Short. tryParse (dr [name]. toString (), out result) throw new Exception ("Short Integer Conversion failed");} return result ;} /// <summary> /// obtain Byte data /// </summary> /// <param name = "dr"> </param> // <param name = "name"> </param> // <returns> </returns> public static byte GetByte (this DataRow dr, string name) {byte result = 0; if (dr [name]! = DBNull. Value & dr [name]! = Null) {if (! Byte. tryParse (dr [name]. toString (), out result) throw new Exception ("failed Byte type conversion");} return result ;} /// <summary> /// obtain the bool type data. If the value is 1, true is returned; /// </summary> /// <param name = "con"> </param> /// <param name = "name"> </param> /// <returns> </returns> public static bool GetBool (this DataRow dr, string name) {if (dr [name]! = DBNull. Value & dr [name]! = Null) {return dr [name]. toString () = "1" | dr [name]. toString () = "yes" | dr [name]. toString (). toLower () = "true";} return false ;}# endregion}