2828//* 日時 更新者 内容
2929//* ---------- ---------------- -------------------------------------------------
3030//* 2018/07/23 西野 大介 新規作成
31+ //* 2018/10/03 西野 大介 性能対策
3132//**********************************************************************************
3233
3334using System ;
3435using System . Data ;
3536using System . Collections . Generic ;
36- using System . Diagnostics ;
37+
38+ using Touryo . Infrastructure . Public . Util ;
3739
3840namespace Touryo . Infrastructure . Public . Dto
3941{
@@ -94,23 +96,94 @@ public Dictionary<string, string> DataTableToDictionary(DataTable dt)
9496 #region DataReader
9597
9698 #region List
97-
99+
98100 /// <summary>DataReaderからDictionary配列に変換する。</summary>
99101 /// <param name="dr">IDataReader</param>
100102 /// <returns>List(Dictionary(string, string))</returns>
101103 public List < Dictionary < string , string > > DataReaderToDictionaryList ( IDataReader dr )
102104 {
105+ // https://stackoverflow.com/questions/373230/check-for-column-name-in-a-sqldatareader-object
106+ HashSet < string > hs = PubCmnFunction . GetDataReaderColumnInfo ( dr ) ;
107+
103108 Dictionary < string , string > obj = null ;
104109 List < Dictionary < string , string > > list = new List < Dictionary < string , string > > ( ) ;
105-
106- do
110+
111+ // IDataReader の既定の位置は、先頭のレコードの前
112+ while ( dr . Read ( ) )
107113 {
108- obj = this . DataReaderToDictionary ( dr ) ;
114+ // Dictionary
115+ obj = new Dictionary < string , string > ( ) ;
116+
117+ // dr.FieldCountで回す。
118+ for ( int i = 0 ; i < dr . FieldCount ; i ++ )
119+ {
120+ string srcPropName = dr . GetName ( i ) ;
121+ string dstPropName = srcPropName ;
122+
123+ // マップの有無
124+ if ( this . Mapping == null )
125+ {
126+ // マップ無
127+ }
128+ else
129+ {
130+ // マップ有
131+ if ( this . Mapping . ContainsKey ( srcPropName ) )
132+ {
133+ // 値あり
134+ dstPropName = this . Mapping [ srcPropName ] ;
135+ }
136+ else
137+ {
138+ // 値なし
139+ }
140+ }
141+
142+ if ( hs . Contains ( srcPropName ) )
143+ {
144+ object o = dr [ srcPropName ] ;
145+
146+ // TimeSpan型の書式指定をする方法と注意点(C#)
147+ // https://ict119.com/timespan_format/#DateTimeTimeSpan
148+ // 2.1 DateTime型とTimeSpan型の書式指定子の違い
149+
150+ if ( o . GetType ( ) == typeof ( DateTime ) )
151+ {
152+ if ( string . IsNullOrEmpty ( this . DateTimeFormat ) )
153+ {
154+ // 精度を保つための仕様
155+ obj . Add ( dstPropName , ( ( DateTime ) o ) . Ticks . ToString ( ) ) ;
156+ }
157+ else
158+ {
159+ // dateTimeFormatでフォーマット
160+ obj . Add ( dstPropName , ( ( DateTime ) o ) . ToString ( this . DateTimeFormat ) ) ;
161+ }
162+ }
163+ else if ( o . GetType ( ) == typeof ( TimeSpan ) )
164+ {
165+ if ( string . IsNullOrEmpty ( this . TimeSpanFormat ) )
166+ {
167+ // 精度を保つための仕様
168+ obj . Add ( dstPropName , ( ( TimeSpan ) o ) . Ticks . ToString ( ) ) ;
169+ }
170+ else
171+ {
172+ // dateTimeFormatでフォーマット
173+ obj . Add ( dstPropName , ( ( TimeSpan ) o ) . ToString ( this . TimeSpanFormat ) ) ;
174+ }
175+ }
176+ else
177+ {
178+ // 通常時
179+ obj . Add ( dstPropName , o . ToString ( ) ) ;
180+ }
181+ }
182+ }
109183
110184 // List(Dictionary(string, string))に追加。
111185 if ( obj != null ) list . Add ( obj ) ;
112186 }
113- while ( obj != null ) ;
114187
115188 // List(Dictionary(string, string))を返す。
116189 return list ;
@@ -128,6 +201,9 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
128201 // drのDataTableスキーマ情報 .net coreで動かない。
129202 //DataTable dt = dr.GetSchemaTable();
130203
204+ // https://stackoverflow.com/questions/373230/check-for-column-name-in-a-sqldatareader-object
205+ HashSet < string > hs = PubCmnFunction . GetDataReaderColumnInfo ( dr ) ;
206+
131207 Dictionary < string , string > obj = null ;
132208
133209 // IDataReader の既定の位置は、先頭のレコードの前
@@ -161,9 +237,9 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
161237 }
162238 }
163239
164- try
240+ if ( hs . Contains ( srcPropName ) )
165241 {
166- object o = dr [ srcPropName ] ; // 検証
242+ object o = dr [ srcPropName ] ;
167243
168244 // TimeSpan型の書式指定をする方法と注意点(C#)
169245 // https://ict119.com/timespan_format/#DateTimeTimeSpan
@@ -182,7 +258,7 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
182258 obj . Add ( dstPropName , ( ( DateTime ) o ) . ToString ( this . DateTimeFormat ) ) ;
183259 }
184260 }
185- else if ( o . GetType ( ) == typeof ( TimeSpan ) )
261+ else if ( o . GetType ( ) == typeof ( TimeSpan ) )
186262 {
187263 if ( string . IsNullOrEmpty ( this . TimeSpanFormat ) )
188264 {
@@ -199,11 +275,7 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
199275 {
200276 // 通常時
201277 obj . Add ( dstPropName , o . ToString ( ) ) ;
202- }
203- }
204- catch ( Exception ex )
205- {
206- Debug . Write ( ex . ToString ( ) ) ;
278+ }
207279 }
208280 }
209281 }
0 commit comments