Friday, May 15, 2009

Creating disconnected ADO Recordset in C#

I had some problems to create a disconnected ADO Recordset in C#. Finally I get it right.

public static Recordset CreateDisconnectedRecordset()
{
    // Create new recordset
    var rs = new Recordset();

    // Add some updatable fields
    rs.Fields.Append("name", DataTypeEnum.adVarChar, 20, FieldAttributeEnum.adFldUpdatable, Missing.Value);
    rs.Fields.Append("country", DataTypeEnum.adVarChar, 20, FieldAttributeEnum.adFldUpdatable, Missing.Value);

    // Open recordset
    rs.Open(Missing.Value, Missing.Value, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, 0);

    // Add data
    rs.AddNew(Missing.Value, Missing.Value);
    rs.Fields["name"].Value = "Anders";
    rs.Fields["country"].Value = "Sweden";
    rs.Update(Missing.Value, Missing.Value);

    return rs;
}