C# DataTable扩展方法

在日常搬砖中,总结了一些简单的扩展方法。

        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
        /// 
        /// 获取字符串
        /// 
        /// 列名
        /// 
        public static string GetStringValue(this DataTable data, int rowIndex, string pColumnName)
        {
            return GetValue(data, rowIndex, pColumnName).ToSafeStr();
        }

        /// 
        /// 获取整型
        /// 
        /// 列名
        /// 
        public static int GetIntValue(this DataTable data, int rowIndex, string pColumnName)
        {
            return GetStringValue(data, rowIndex, pColumnName).ToInt();
        }

        /// 
        /// 获取浮点型
        /// 
        /// 列名
        /// 
        public static float GetFloatValue(this DataTable data, int rowIndex, string pColumnName)
        {
            return GetStringValue(data, rowIndex, pColumnName).ToFloat();
        }

        /// 
        /// 获取Boolean
        /// 
        /// 列名
        /// 
        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);
        }

        /// 
        /// 判断DataTable 是否为空
        /// 
        public static bool IsNullOrEmpty(this DataTable data)
        {
            return data == null || data.Rows.Count <= 0;
        }

        /// 
        /// 判断DataTable 不为空
        /// 
        public static bool IsNotEmpty(this DataTable data)
        {
            return data.IsNullOrEmpty() == false;
        }
        #endregion

 

        #region DataRow Extension
        /// 
        /// 获取字符串
        /// 
        /// 列名
        /// 
        public static string GetStringValue(this DataRow row, string pColumnName)
        {
            return GetValue(row, pColumnName).ToSafeStr();
        }

        /// 
        /// 获取整型
        /// 
        /// 列名
        /// 
        public static int GetIntValue(this DataRow row, string pColumnName)
        {
            return GetStringValue(row, pColumnName).ToInt();
        }

        /// 
        /// 获取浮点型
        /// 
        /// 列名
        /// 
        public static float GetFloatValue(this DataRow row, string pColumnName)
        {
            return GetStringValue(row, pColumnName).ToFloat();
        }

        /// 
        /// 获取Boolean
        /// 
        /// 列名
        /// 
        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

 

转载于:https://www.cnblogs.com/kikyoqiang/p/9533393.html

你可能感兴趣的:(C# DataTable扩展方法)