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

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

Post by Q2hyaX » Tue, 07 Feb 2006 23:30:11


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.FieldAttribu *** um.adFldMayBeNull, null);
rs.Fields.Append("id",ADODB.DataTypeEnum.adVarChar ,16
,ADODB.FieldAttribu *** um.adFldMayBeNull, null);
rs.Fields.Append("time", ADODB.DataTypeEnum.adVarChar, 10,
ADODB.FieldAttribu *** um.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.

Thanks,
Chris
 
 
 

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

Post by Daniel Cri » Wed, 08 Feb 2006 01:20:27

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


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.FieldAttribu *** um.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

 
 
 

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

Post by Daniel Cri » Wed, 08 Feb 2006 01:22:06

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


Gah, ignore my previous reply about the creation of fields. There is no way
to set the value at creation time - you're creating a column defintion, not
a row in the recordset. You set the values when you add a new record.

Dan
 
 
 

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

Post by QnJlbnQgUm » Sat, 04 Mar 2006 02:30:31

I have a similar issue to Chris in that I need to open an "Unconnected"
recordset. Yet I cannot find a way to pass a null connection. Daniel's
suggestion to use

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

Won't work as this generates a compile error.