반응형
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");
//...
}
반응형
'윈폼(Winform)' 카테고리의 다른 글
C# Round Border Fill Panel (0) | 2021.11.18 |
---|---|
C# 부모 윈도우의 중앙에 메세지박스 띄우기 (0) | 2021.11.18 |
C# DataBase(Mysql, Maria) 접속, Select/Execute Query (0) | 2021.11.18 |
C# Round Border Label (0) | 2021.11.17 |
C# Crystal Report 예제(편집, 인쇄) (1) | 2021.11.17 |
최근댓글