Skip to content

Commit

Permalink
fixed #313
Browse files Browse the repository at this point in the history
  • Loading branch information
daisukenishino2 committed Oct 15, 2018
1 parent 5d909b9 commit f30aad7
Show file tree
Hide file tree
Showing 14 changed files with 659 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
//* 日時 更新者 内容
//* ---------- ---------------- -------------------------------------------------
//* 2018/07/23 西野 大介 新規作成
//* 2018/10/03 西野 大介 性能対策
//**********************************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Diagnostics;

using Touryo.Infrastructure.Public.Util;

namespace Touryo.Infrastructure.Public.Dto
{
Expand Down Expand Up @@ -94,23 +96,94 @@ public Dictionary<string, string> DataTableToDictionary(DataTable dt)
#region DataReader

#region List

/// <summary>DataReaderからDictionary配列に変換する。</summary>
/// <param name="dr">IDataReader</param>
/// <returns>List(Dictionary(string, string))</returns>
public List<Dictionary<string, string>> DataReaderToDictionaryList(IDataReader dr)
{
// https://stackoverflow.com/questions/373230/check-for-column-name-in-a-sqldatareader-object
HashSet<string> hs = PubCmnFunction.GetDataReaderColumnInfo(dr);

Dictionary<string, string> obj = null;
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();

do

// IDataReader の既定の位置は、先頭のレコードの前
while (dr.Read())
{
obj = this.DataReaderToDictionary(dr);
// Dictionary
obj = new Dictionary<string, string>();

// dr.FieldCountで回す。
for (int i = 0; i < dr.FieldCount; i++)
{
string srcPropName = dr.GetName(i);
string dstPropName = srcPropName;

// マップの有無
if (this.Mapping == null)
{
// マップ無
}
else
{
// マップ有
if (this.Mapping.ContainsKey(srcPropName))
{
// 値あり
dstPropName = this.Mapping[srcPropName];
}
else
{
// 値なし
}
}

if (hs.Contains(srcPropName))
{
object o = dr[srcPropName];

// TimeSpan型の書式指定をする方法と注意点(C#)
// https://ict119.com/timespan_format/#DateTimeTimeSpan
// 2.1 DateTime型とTimeSpan型の書式指定子の違い

if (o.GetType() == typeof(DateTime))
{
if (string.IsNullOrEmpty(this.DateTimeFormat))
{
// 精度を保つための仕様
obj.Add(dstPropName, ((DateTime)o).Ticks.ToString());
}
else
{
// dateTimeFormatでフォーマット
obj.Add(dstPropName, ((DateTime)o).ToString(this.DateTimeFormat));
}
}
else if (o.GetType() == typeof(TimeSpan))
{
if (string.IsNullOrEmpty(this.TimeSpanFormat))
{
// 精度を保つための仕様
obj.Add(dstPropName, ((TimeSpan)o).Ticks.ToString());
}
else
{
// dateTimeFormatでフォーマット
obj.Add(dstPropName, ((TimeSpan)o).ToString(this.TimeSpanFormat));
}
}
else
{
// 通常時
obj.Add(dstPropName, o.ToString());
}
}
}

// List(Dictionary(string, string))に追加。
if (obj != null) list.Add(obj);
}
while (obj != null);

// List(Dictionary(string, string))を返す。
return list;
Expand All @@ -128,6 +201,9 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
// drのDataTableスキーマ情報 .net coreで動かない。
//DataTable dt = dr.GetSchemaTable();

// https://stackoverflow.com/questions/373230/check-for-column-name-in-a-sqldatareader-object
HashSet<string> hs = PubCmnFunction.GetDataReaderColumnInfo(dr);

Dictionary<string, string> obj = null;

// IDataReader の既定の位置は、先頭のレコードの前
Expand Down Expand Up @@ -161,9 +237,9 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
}
}

try
if (hs.Contains(srcPropName))
{
object o = dr[srcPropName]; // 検証
object o = dr[srcPropName];

// TimeSpan型の書式指定をする方法と注意点(C#)
// https://ict119.com/timespan_format/#DateTimeTimeSpan
Expand All @@ -182,7 +258,7 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
obj.Add(dstPropName, ((DateTime)o).ToString(this.DateTimeFormat));
}
}
else if(o.GetType() == typeof(TimeSpan))
else if (o.GetType() == typeof(TimeSpan))
{
if (string.IsNullOrEmpty(this.TimeSpanFormat))
{
Expand All @@ -199,11 +275,7 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
{
// 通常時
obj.Add(dstPropName, o.ToString());
}
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
}
}
Expand Down
Loading

0 comments on commit f30aad7

Please sign in to comment.