2828//* 日時 更新者 内容
2929//* ---------- ---------------- -------------------------------------------------
3030//* 2018/07/23 西野 大介 新規作成
31+ //* 2018/10/03 西野 大介 性能対策
32+ //* 2018/10/23 西野 大介 微調整
3133//**********************************************************************************
3234
3335using System ;
3436using System . Data ;
3537using System . Collections . Generic ;
36- using System . Diagnostics ;
38+
39+ using Touryo . Infrastructure . Public . Util ;
3740
3841namespace Touryo . Infrastructure . Public . Dto
3942{
@@ -94,23 +97,94 @@ public Dictionary<string, string> DataTableToDictionary(DataTable dt)
9497 #region DataReader
9598
9699 #region List
97-
100+
98101 /// <summary>DataReaderからDictionary配列に変換する。</summary>
99102 /// <param name="dr">IDataReader</param>
100103 /// <returns>List(Dictionary(string, string))</returns>
101104 public List < Dictionary < string , string > > DataReaderToDictionaryList ( IDataReader dr )
102105 {
106+ // https://stackoverflow.com/questions/373230/check-for-column-name-in-a-sqldatareader-object
107+ HashSet < string > hs = PubCmnFunction . GetDataReaderColumnInfo ( dr ) ;
108+
103109 Dictionary < string , string > obj = null ;
104110 List < Dictionary < string , string > > list = new List < Dictionary < string , string > > ( ) ;
105-
106- do
111+
112+ // IDataReader の既定の位置は、先頭のレコードの前
113+ while ( dr . Read ( ) )
107114 {
108- obj = this . DataReaderToDictionary ( dr ) ;
115+ // Dictionary
116+ obj = new Dictionary < string , string > ( ) ;
117+
118+ // dr.FieldCountで回す。
119+ for ( int i = 0 ; i < dr . FieldCount ; i ++ )
120+ {
121+ string srcPropName = dr . GetName ( i ) ;
122+ string dstPropName = srcPropName ;
123+
124+ // マップの有無
125+ if ( this . Mapping == null )
126+ {
127+ // マップ無
128+ }
129+ else
130+ {
131+ // マップ有
132+ if ( this . Mapping . ContainsKey ( srcPropName ) )
133+ {
134+ // 値あり
135+ dstPropName = this . Mapping [ srcPropName ] ;
136+ }
137+ else
138+ {
139+ // 値なし
140+ }
141+ }
142+
143+ if ( hs . Contains ( srcPropName ) )
144+ {
145+ object o = dr [ srcPropName ] ;
146+
147+ // TimeSpan型の書式指定をする方法と注意点(C#)
148+ // https://ict119.com/timespan_format/#DateTimeTimeSpan
149+ // 2.1 DateTime型とTimeSpan型の書式指定子の違い
150+
151+ if ( o is DateTime )
152+ {
153+ if ( string . IsNullOrEmpty ( this . DateTimeFormat ) )
154+ {
155+ // 精度を保つための仕様
156+ obj . Add ( dstPropName , ( ( DateTime ) o ) . Ticks . ToString ( ) ) ;
157+ }
158+ else
159+ {
160+ // dateTimeFormatでフォーマット
161+ obj . Add ( dstPropName , ( ( DateTime ) o ) . ToString ( this . DateTimeFormat ) ) ;
162+ }
163+ }
164+ else if ( o is TimeSpan )
165+ {
166+ if ( string . IsNullOrEmpty ( this . TimeSpanFormat ) )
167+ {
168+ // 精度を保つための仕様
169+ obj . Add ( dstPropName , ( ( TimeSpan ) o ) . Ticks . ToString ( ) ) ;
170+ }
171+ else
172+ {
173+ // dateTimeFormatでフォーマット
174+ obj . Add ( dstPropName , ( ( TimeSpan ) o ) . ToString ( this . TimeSpanFormat ) ) ;
175+ }
176+ }
177+ else
178+ {
179+ // 通常時
180+ obj . Add ( dstPropName , o . ToString ( ) ) ;
181+ }
182+ }
183+ }
109184
110185 // List(Dictionary(string, string))に追加。
111186 if ( obj != null ) list . Add ( obj ) ;
112187 }
113- while ( obj != null ) ;
114188
115189 // List(Dictionary(string, string))を返す。
116190 return list ;
@@ -128,6 +202,9 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
128202 // drのDataTableスキーマ情報 .net coreで動かない。
129203 //DataTable dt = dr.GetSchemaTable();
130204
205+ // https://stackoverflow.com/questions/373230/check-for-column-name-in-a-sqldatareader-object
206+ HashSet < string > hs = PubCmnFunction . GetDataReaderColumnInfo ( dr ) ;
207+
131208 Dictionary < string , string > obj = null ;
132209
133210 // IDataReader の既定の位置は、先頭のレコードの前
@@ -161,15 +238,15 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
161238 }
162239 }
163240
164- try
241+ if ( hs . Contains ( srcPropName ) )
165242 {
166- object o = dr [ srcPropName ] ; // 検証
243+ object o = dr [ srcPropName ] ;
167244
168245 // TimeSpan型の書式指定をする方法と注意点(C#)
169246 // https://ict119.com/timespan_format/#DateTimeTimeSpan
170247 // 2.1 DateTime型とTimeSpan型の書式指定子の違い
171248
172- if ( o . GetType ( ) == typeof ( DateTime ) )
249+ if ( o is DateTime )
173250 {
174251 if ( string . IsNullOrEmpty ( this . DateTimeFormat ) )
175252 {
@@ -182,7 +259,7 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
182259 obj . Add ( dstPropName , ( ( DateTime ) o ) . ToString ( this . DateTimeFormat ) ) ;
183260 }
184261 }
185- else if ( o . GetType ( ) == typeof ( TimeSpan ) )
262+ else if ( o is TimeSpan )
186263 {
187264 if ( string . IsNullOrEmpty ( this . TimeSpanFormat ) )
188265 {
@@ -199,11 +276,7 @@ public Dictionary<string, string> DataReaderToDictionary(IDataReader dr)
199276 {
200277 // 通常時
201278 obj . Add ( dstPropName , o . ToString ( ) ) ;
202- }
203- }
204- catch ( Exception ex )
205- {
206- Debug . Write ( ex . ToString ( ) ) ;
279+ }
207280 }
208281 }
209282 }
0 commit comments