IDataRecord Fields to Dictionary Extension Method

I have never been a fan of directly passing IDataRecords, or IDataReaders for that matter, about the place to get simple field values out.
Therefore, with the introduction of C# 3.0 and Extension Methods, I thought it would be cool to write (and share) a simple implementation of some code that I use to convert the IDataRecord Field data to an Dictionary<string, object> object.
namespace Kinlan.Data.Extensions{ public static class DataExtensions { public static Dictionary<string, object> FieldsToDictionary(this IDataRecord dataRecord) { Dictionary<string, object> fieldBag = new Dictionary<string, object>(dataRecord.


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan

<p>I have never been a fan of directly passing IDataRecords, or IDataReaders for that matter, about the place to get simple field values out.</p> <p>Therefore, with the introduction of C# 3.0 and Extension Methods, I thought it would be cool to write (and share) a simple implementation of some code that I use to convert the IDataRecord Field data to an Dictionary<string, object> object.</p><div class="CodeRay"> <div class="code"><pre>namespace Kinlan.Data.Extensions{ public static class DataExtensions { public static Dictionary<string, object> FieldsToDictionary(this IDataRecord dataRecord) { Dictionary<string, object> fieldBag = new Dictionary<string, object>(dataRecord.FieldCount); if (dataRecord != null) { for (int fieldIdx = 0; fieldIdx < dataRecord.FieldCount; fieldIdx++) { string name = dataRecord.GetName(fieldIdx); object value = dataRecord[fieldIdx]; fieldBag.Add(name, value); } } return fieldBag; } }}</pre></div> </div> <p>It is quite simple really and nothing too complex.</p><p>A place where it can be used it Windows Workflow.  If you are injecting parameters into your Workflow instance you need to pass a Dictionary<string, object> in, well now you can (if you desired) simply convert a IDataReader/IDataRecord object into with the following simple piece of code:</p><div class="CodeRay"> <div class="code"><pre>WorkflowInstance instance = runtime.CreateWorkflow(typeof(_WorkflowClass_), dataReaderInstance.FieldsToDictionary());</pre></div> </div> <p>This code should be used sparingly, for instance if you wanted a very high performance access to the field data, you might as well stay on the IDataRecord.</p><div class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px;">Topicala Tags: <a href="http://www.topicala.com/tag/Extension%20Method" rel="tag">Extension Method</a>, <a href="http://www.topicala.com/tag/ExtensionMethod" rel="tag">ExtensionMethod</a>, <a href="http://www.topicala.com/tag/C#" rel="tag">C#</a>, <a href="http://www.topicala.com/tag/C#3.0" rel="tag">C#3.0</a>, <a href="http://www.topicala.com/tag/IDataReader" rel="tag">IDataReader</a>, <a href="http://www.topicala.com/tag/IDataRecord" rel="tag">IDataRecord</a> </div>


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan


Print Share Comment Cite Upload Translate Updates
APA

Paul Kinlan | Sciencx (2007-12-18T00:00:00+00:00) IDataRecord Fields to Dictionary Extension Method. Retrieved from https://www.scien.cx/2007/12/18/idatarecord-fields-to-dictionary-extension-method/

MLA
" » IDataRecord Fields to Dictionary Extension Method." Paul Kinlan | Sciencx - Tuesday December 18, 2007, https://www.scien.cx/2007/12/18/idatarecord-fields-to-dictionary-extension-method/
HARVARD
Paul Kinlan | Sciencx Tuesday December 18, 2007 » IDataRecord Fields to Dictionary Extension Method., viewed ,<https://www.scien.cx/2007/12/18/idatarecord-fields-to-dictionary-extension-method/>
VANCOUVER
Paul Kinlan | Sciencx - » IDataRecord Fields to Dictionary Extension Method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2007/12/18/idatarecord-fields-to-dictionary-extension-method/
CHICAGO
" » IDataRecord Fields to Dictionary Extension Method." Paul Kinlan | Sciencx - Accessed . https://www.scien.cx/2007/12/18/idatarecord-fields-to-dictionary-extension-method/
IEEE
" » IDataRecord Fields to Dictionary Extension Method." Paul Kinlan | Sciencx [Online]. Available: https://www.scien.cx/2007/12/18/idatarecord-fields-to-dictionary-extension-method/. [Accessed: ]
rf:citation
» IDataRecord Fields to Dictionary Extension Method | Paul Kinlan | Sciencx | https://www.scien.cx/2007/12/18/idatarecord-fields-to-dictionary-extension-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.