在日常搬砖中,总结了一些简单的扩展方法。
public static bool IsNullOrEmpty(this DataTable dt) { return dt == null || dt.Rows.Count <= 0; } public static bool IsNotEmpty(this DataTable dt) { return dt.IsNullOrEmpty() == false; }
#region DataTable Extension /// <summary> /// 获取字符串 /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static string GetStringValue(this DataTable data, int rowIndex, string pColumnName) { return GetValue(data, rowIndex, pColumnName).ToSafeStr(); } /// <summary> /// 获取整型 /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static int GetIntValue(this DataTable data, int rowIndex, string pColumnName) { return GetStringValue(data, rowIndex, pColumnName).ToInt(); } /// <summary> /// 获取浮点型 /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static float GetFloatValue(this DataTable data, int rowIndex, string pColumnName) { return GetStringValue(data, rowIndex, pColumnName).ToFloat(); } /// <summary> /// 获取Boolean /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static bool GetBooleanValue(this DataTable data, int rowIndex, string pColumnName) { string v = GetStringValue(data, rowIndex, pColumnName).ToLower(); return v.ToBoolean(); } public static decimal GetDecimalValue(this DataTable data, int rowIndex, string pColumnName) { return GetStringValue(data, rowIndex, pColumnName).ToDecimal(); } public static object GetValue(this DataTable data, int rowIndex, string pColumnName) { if (data == null || rowIndex < 0 || rowIndex >= data.Rows.Count) return null; return data.Rows[rowIndex].GetValue(pColumnName); } /// <summary> /// 判断DataTable 是否为空 /// </summary> public static bool IsNullOrEmpty(this DataTable data) { return data == null || data.Rows.Count <= 0; } /// <summary> /// 判断DataTable 不为空 /// </summary> public static bool IsNotEmpty(this DataTable data) { return data.IsNullOrEmpty() == false; } #endregion
#region DataRow Extension /// <summary> /// 获取字符串 /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static string GetStringValue(this DataRow row, string pColumnName) { return GetValue(row, pColumnName).ToSafeStr(); } /// <summary> /// 获取整型 /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static int GetIntValue(this DataRow row, string pColumnName) { return GetStringValue(row, pColumnName).ToInt(); } /// <summary> /// 获取浮点型 /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static float GetFloatValue(this DataRow row, string pColumnName) { return GetStringValue(row, pColumnName).ToFloat(); } /// <summary> /// 获取Boolean /// </summary> /// <param name="pColumnName">列名</param> /// <returns></returns> public static bool GetBooleanValue(this DataRow row, string pColumnName) { string v = GetStringValue(row, pColumnName); return v.ToBoolean(); } public static object GetValue(this DataRow row, string pColumnName) { if (row == null) return null; if (pColumnName.IsNullOrEmpty()) return null; if (!row.Table.Columns.Contains(pColumnName)) return null; return row[pColumnName]; } #endregion