Pages

Create deep copy of recordset (C#)

Here is the function to create deep copy of recordset in C# (for VB code click here) :-



public static ADODB.Recordset RSDeepCopy(ADODB.Recordset RSSrc)
{
ADODB.Recordset rsCopy = new ADODB.Recordset();

try
{
//Take care of Null recordSet
if ((RSSrc == null))
{
return null;
}
//Either Open Closed RecordSet Or Note its Position if Already Open
long AbsPos = -100;
if ((RSSrc.State == 0))
{
RSSrc.Open();
}
else
{
AbsPos = (long)RSSrc.AbsolutePosition;
}

//Go to Begining of Recordset
if ((RSSrc.RecordCount > 0))
{
RSSrc.MoveFirst();
}


//Copy Source Recordset to Stream
ADODB.Stream RSStrm = new ADODB.Stream();
RSStrm.Type = ADODB.StreamTypeEnum.adTypeBinary;
RSSrc.Save(RSStrm);

//Load Stream into RecordSet Mirror

rsCopy.Open(RSStrm);

//Close the Stream(Not Required)
RSStrm.Close();

//If Recordset was open restore its position otherwise close
if ((AbsPos != -100))
{
if ((RSSrc.RecordCount > 0))
{
if ((AbsPos != -3))
{
RSSrc.AbsolutePosition = (ADODB.PositionEnum)AbsPos;
}
else
{
RSSrc.MoveLast();
RSSrc.MoveNext();
}
}
}
else
{
RSSrc.Close();
}

//Not closind Cloned Recordset
//rsCopy.Close()

//Stream is not Required
RSStrm = null;

}
catch (Exception ex)
{
}
//Return the Cloned Recordset
return rsCopy;
}