반응형

DataTable의 DataRow에서 각 항목을 데이터 타입별로 가져오는 유틸

  • Integer, Double, String, DateTime 타입 지원
  • DataBase에서 가져온 DataTable의 값을 가져오는데 사용

 

DBUtil.cs

using System;
using System.Data;

namespace SrcTree.Util {
    class DBUtil {
        public static string toString(DataRow row, string sColumnName, string sDefaultValue) {
            string sResult = sDefaultValue;
            if(row.Table.Columns.Contains(sColumnName)) {
                object value = row[sColumnName];
                if((value is DBNull) == false && value != null) {
                    sResult = value.ToString();
                }
            }
            sResult = sResult.Trim();
            return sResult;
        }
        public static string toString(DataRow row, string sColumnName) {
            return toString(row, sColumnName, string.Empty);
        }

        public static int toInteger(DataRow row, string sColumnName, int nDefaultValue) {
            int nResult = nDefaultValue;
            if(row.Table.Columns.Contains(sColumnName)) {
                object value = row[sColumnName];
                if((value is DBNull) == false && value != null) {
                    string sValue = value.ToString();
                    if(int.TryParse(sValue, out nResult) == false)
                        nResult = nDefaultValue;
                }
            }
            return nResult;
        }
        public static int toInteger(DataRow row, string sColumnName) {
            return toInteger(row, sColumnName, 0);
        }

        public static double toDouble(DataRow row, string sColumnName, double dDefaultValue) {
            double dResult = dDefaultValue;
            if(row.Table.Columns.Contains(sColumnName)) {
                object value = row[sColumnName];
                if((value is DBNull) == false && value != null) {
                    string sValue = value.ToString();
                    if(double.TryParse(sValue, out dResult) == false)
                        dResult = dDefaultValue;
                }
            }
            return dResult;
        }
        public static double toDouble(DataRow row, string sColumnName) {
            return toDouble(row, sColumnName, 0);
        }

        public static DateTime toDateTime(DataRow row, string sColumnName, DateTime defaultTime) {
            DateTime result = defaultTime;

            if(row.Table.Columns.Contains(sColumnName)) {
                object value = row[sColumnName];
                if((value is DBNull) == false && value != null) {
                    try {
                        result = (DateTime)value;
                    }
                    catch(Exception ex) {
                        Console.Write(ex);
                    }
                }
            }
            return result;
        }
        public static DateTime toDateTime(DataRow row, string sColumnName) {
            return toDateTime(row, sColumnName, DateTime.MinValue);
        }
    }
}

 

사용

string query = "select * from tbl_test";
DataTable dt = MariaUtil.instance.select(query);

foreach(DataRow row in dt.Rows) { 
    int seq = DBUtil.toInteger(row, "seq");
    String data = DBUtil.toString(row, "data");
    DateTime time = DBUtil.toDataTime(row, "time");
    //...
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기