Re: ADO 2.7 C# - How Do I Create a Recordset On-the-Fly?



Chris wrote on Mon, 6 Feb 2006 06:30:11 -0800:

I'm using ADO 2.7 for compatibility with an existing component I'm using.
I'm trying to add data to a recordset that I've created on-the-fly using
C# .NET 2003 (I'm new to C#, so please bear with me...). rs.Open requires
a Source and Active Connection parameter. Since the recordset is created
and populated from code, I'm not sure how to satisfy these two parameters.

In a nutshell, here's what I'm trying to do:

rs.Fields.Append("type", ADODB.DataTypeEnum.adVarChar, 1,
ADODB.FieldAttributeEnum.adFldMayBeNull, null);
rs.Fields.Append("id",ADODB.DataTypeEnum.adVarChar ,16
,ADODB.FieldAttributeEnum.adFldMayBeNull, null);
rs.Fields.Append("time", ADODB.DataTypeEnum.adVarChar, 10,
ADODB.FieldAttributeEnum.adFldMayBeNull, null);

object[] f = new object[3];
object[] v = new object[3];

f[0] = "type";
f[1] = "id";
f[2] = "time";

v[0] = type;
v[1] = ID;
v[2] = time;

object x = new object();
object y = new object();
x = null;
y = null;

rs.Open(x, y,
ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockOptimistic,-1);
rs.AddNew(f,v);
rs.Update(f,v);

The null assignment for Source and Active Connection generate an error
when passed to rs.Open.

My other question is what is expected for FieldValue in the
rs.Fields.Append? Null is the only value I can pass without creating an
"invalid arguement" error.

I've not tried in C#, but in VB6 I can see 2 reasons why it won't work:

(1) the options for the field is the last value in the method call for
creating a field, so you're getting the argument error because you're trying
to set the attributes property to something that isn't one of the enums.

eg.

rs.Fields.Append("type", ADODB.DataTypeEnum.adVarChar, 1, "A",
ADODB.FieldAttributeEnum.adFldMayBeNull);

if the "type" field should be set to the letter "A".

(2) the Open method can have the source and connection properties left
blank. Don't pass null, just leave them completely empty

eg.

rs.Open(, ,
ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockOptimistic,-1);

or

rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic;
rs.LockType = ADODB.LockTypeEnum.adLockOptimistic;
rs.Open();

I'm not sure if this is valid syntax for C#, I just copied your code and
modified it to have similar syntax to the way it's done in VB.

Dan


.



Relevant Pages

  • ADO 2.7 C# - How Do I Create a Recordset On-the-Fly?
    ... I'm using ADO 2.7 for compatibility with an existing component I'm using. ... I'm trying to add data to a recordset that I've created on-the-fly using C# ... Source and Active Connection parameter. ...
    (microsoft.public.data.ado)
  • Re: ADO 2.7 C# - How Do I Create a Recordset On-the-Fly?
    ... I'm trying to add data to a recordset that I've created on-the-fly using ... a Source and Active Connection parameter. ... The null assignment for Source and Active Connection generate an error ... to set the value at creation time - you're creating a column defintion, ...
    (microsoft.public.data.ado)
  • Re: Disconnected Recordsets with Command Object
    ... that the Active Connection could not be changed because the Recordset had a ... the same thing but in fact each way behaves just a little differently. ... >>How do I create a disconnected recordset using the command object? ...
    (microsoft.public.data.ado)