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;
}
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.
Pingback: How do I populate an ado recordset with data from a generic list programmatically from .net | PHP Developer Resource
Hey! I could have sworn I’ve been to this site before but after reading through some of the post I realized it’s new to me.
Anyways, I’m definitely delighted I found it and I’ll be book-marking and checking back frequently!