How to use the .Net 4.0 Dynamic Keyword to convert a recordset to an object

This is a simple example of how to convert an old style ADODB recordset into a dynamic object using the .Net 4.0 dynamic keyword.

This example assumes that you have the ADODB referenced as an Interop.


            public static List<dynamic> AsDynaList(ADODB.Recordset recordset)
            {
              
                List<dynamic> rs = new List<dynamic>();
                while (!recordset.EOF)
                {
                    var fieldIndex = 0;
                    var test = new ExpandoObject() as IDictionary<string, object>;

                    foreach (ADODB.Field fld in recordset.Fields)
                    {
                        test.Add(fld.Name, recordset.Fields[fieldIndex].Value);
                        fieldIndex++;
                    }
                    rs.Add(test);
                    recordset.MoveNext();
                }
                return rs;
            }

One thought on “How to use the .Net 4.0 Dynamic Keyword to convert a recordset to an object

  1. Thanks for this snipet of code. It was exactly what I needed to integrate with a legacy application. Saved me lots of time.

    Thanks again.

    fdlane.

Leave a Reply

Your email address will not be published.


2 − one =

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>