.net DataTable 取值辅助类

DataTableCommon类主要是帮助取值

方法列表:

public static string GetCellString(DataTable dt,int row, int column)

public static string GetCellString(DataTable dt,int row, string columnName)

public static int GetCellInt(DataTable dt, int row, int column)

public static int GetCellInt(DataTable dt, int row, string columnName)

public static decimal GetCellDecimal(DataTable dt, int row, int column)

public static decimal GetCellDecimal(DataTable dt, int row,string columnName)

 public class DataTableComon
    {
        #region DataTable 取值
        
       
        public static string GetCellString(DataTable dt, int row, int column)
        {
            string strValue = string.Empty;
            try
            {
                if (dt == null || dt.Rows.Count == 0)
                    return null;
                object objValue = dt.Rows[row][column];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return null;
                strValue = objValue.ToString();
            }
            catch (Exception)
            {
            }
            return strValue;
        }
        public static string GetCellString(DataTable dt, int row, string columnName)
        {
            string strValue = string.Empty;
            try
            {
                if (dataRow == null || dataRow.Rows.Count == 0)
                    return null;
                object objValue = dataRow.Rows[row][columnName];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return null;
                strValue = objValue.ToString();
            }
            catch (Exception)
            {
            }
            return strValue;
        }
        public static int GetCellInt(DataTable dt, int row, int column)
        {
            int intValue = 0;
            try
            {
                if (dt == null || dt.Rows.Count == 0)
                    return 0;
                object objValue = dt.Rows[row][column];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToInt32(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }
        public static int GetCellInt(DataTable dt, int row, string columnName)
        {
            int intValue = 0;
            try
            {
                if (dt == null || dt.Rows.Count == 0)
                    return 0;
                object objValue = dt.Rows[row][columnName];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToInt32(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }

        public static decimal GetCellDecimal(DataTable dt, int row, int column)
        {
            decimal intValue = 0;
            try
            {
                if (dt == null || dt.Rows.Count == 0)
                    return 0;
                object objValue = dt.Rows[row][column];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToDecimal(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }
        public static decimal GetCellDecimal(DataTable dt, int row, string columnName)
        {
            decimal intValue = 0;
            try
            {
                if (dt == null || dt.Rows.Count == 0)
                    return 0;
                object objValue = dt.Rows[row][columnName];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToDecimal(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }

        #endregion


        #region DataRow 取值

       
        public static string GetCellString(DataRow dataRow, int column)
        {
            string strValue = string.Empty;
            try
            {
                if (dataRow == null)
                    return null;
                object objValue = dataRow[column];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return null;
                strValue = objValue.ToString();
            }
            catch (Exception)
            {
            }
            return strValue;
        }
        public static string GetCellString(DataRow dataRow,string columnName)
        {
            string strValue = string.Empty;
            try
            {
                if (dataRow == null)
                    return null;
                object objValue = dataRow[columnName];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return null;
                strValue = objValue.ToString();
            }
            catch (Exception)
            {
            }
            return strValue;
        }
        public static int GetCellInt(DataRow dataRow, int column)
        {
            int intValue = 0;
            try
            {
                if (dataRow == null )
                    return 0;
                object objValue = dataRow[column];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToInt32(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }
        public static int GetCellInt(DataRow dataRow, string columnName)
        {
            int intValue = 0;
            try
            {
                if (dataRow == null)
                    return 0;
                object objValue = dataRow[columnName];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToInt32(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }

        public static decimal GetCellDecimal(DataRow dataRow,int column)
        {
            decimal intValue = 0;
            try
            {
                if (dataRow == null )
                    return 0;
                object objValue = dataRow[column];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToDecimal(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }
        public static decimal GetCellDecimal(DataRow dataRow, string columnName)
        {
            decimal intValue = 0;
            try
            {
                if (dataRow == null)
                    return 0;
                object objValue = dataRow[columnName];
                if (objValue == null || Convert.IsDBNull(objValue))
                    return 0;
                intValue = Convert.ToDecimal(objValue);
            }
            catch (Exception)
            {
            }
            return intValue;
        }

        #endregion
    }

 

转载于:https://www.cnblogs.com/Dylanblogs/p/4299782.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值