Introduction
I came across the other day with a need to create a .Net interface to allow a COM client to use .NET Remoting. The COM client use ADO Recordset heavily.
When using remoting it is not possible to send a ADO recordset as a return value. The same will apply when using a web service.
I tried googling on the subject but the only article I found was to save the Recordset as a DataSet then save it back to a ADODB Recordset. There had to be a easier solution. After trial and errors. I found the perfect solution (it could be adapted easily for web service, simply uuEncode and decode the byte array)
Here it is, with just a few lines of code....
The Code
<pre>
using System;
using ADODB;
namespace InteropSample
{
public static class RsUtil
{
public static byte[] SerializeRs(Recordset rs)
{
ADODB.Stream s = new ADODB.Stream();
rs.Save(s, ADODB.PersistFormatEnum.adPersistADTG);
return (byte[])s.Read(s.Size);
}
public static Recordset DeserializeRs(byte[] data)
{
ADODB.Stream s = new ADODB.Stream();
s.Open(Missing.Value, ConnectModeEnum.adModeUnknown,
ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, "", "");
s.Type = ADODB.StreamTypeEnum.adTypeBinary;
s.Write(data);
s.Position = 0;
Recordset rs = new Recordset();
rs.Open(s, Missing.Value, CursorTypeEnum.adOpenUnspecified,
LockTypeEnum.adLockUnspecified, -1);
return rs;
}
}
}