• 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
            /// <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

  • 相关阅读:
    windows 环境下在anaconda 3中安装python2和python3两个环境(python2和python3共存)
    python 中,如何在一个函数中调用另一个函数返回的多个值中的一个?
    Coursera 机器学习 第一周 学习笔记
    正则表达式 注释
    正则表达式 分组
    24_04SpringMVC实现文件上传及拦截器
    前端之CSS基础
    前端之html
    MySQL数据库实战之优酷
    数据库基础加练习
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/9533393.html
Copyright © 2020-2023  润新知