Reputation: 73
I am trying to create a new C#.Net COM+ app to communicate with legacy VB6 apps. So far I have everything working I can return all sorts of objects including an ADODB.Recordset.
However I can only add one row of data to that Recordset and if I try to add extra rows only the last row is actually returned to the VB6 app.
// List of data
List<string> names = new List<string> { "Fred", "John", "Jane" };
// Create ADODB Recordset
var adors = new ADODB.Recordset();
adors.Fields.Append("name", ADODB.DataTypeEnum.adLongVarChar, 250, FieldAttributeEnum.adFldIsNullable);
adors.Open(System.Reflection.Missing.Value, System.Reflection.Missing.Value, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic, -1);
// Loop through data and add a row for each one
foreach (var name in names)
{
adors.AddNew();
adors.Fields["name"].Value = name;
adors.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}
// Return Recordset
return adors;
For the above example the VB6 would receive a Recordset back with 1 row of data which would be "Jane"
Could someone please explain to me how to add multiple rows to an ADODB.Recordset in C#?
Upvotes: 1
Views: 1867
Reputation: 73
A little more playing around and I was able to find an answer. You need to set the Recordsets AbsolutePosition to 1.
// Set recordset back to starting position
adors.AbsolutePosition = (PositionEnum)1;
Full example below
// List of data
List<string> names = new List<string> { "Fred", "John", "Jane" };
// Create ADODB Recordset
var adors = new ADODB.Recordset();
adors.Fields.Append("name", ADODB.DataTypeEnum.adLongVarChar, 250, FieldAttributeEnum.adFldIsNullable);
adors.Open(System.Reflection.Missing.Value, System.Reflection.Missing.Value, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic, -1);
// Loop through data and add a row for each one
foreach (var name in names)
{
adors.AddNew();
adors.Fields["name"].Value = name;
adors.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}
// Set recordset back to starting position
adors.AbsolutePosition = (PositionEnum)1;
// Return Recordset
return adors;
Upvotes: 2