Click here to Skip to main content
Licence CPOL
First Posted 26 Sep 2006
Views 22,371
Bookmarked 11 times

ADODB Serialization

By | 26 Sep 2006 | Article
How to serialize and deserialize an ADODB Recordset.

Introduction

I came across a need to create a .NET interface to allow a COM client to use .NET Remoting. The COM client heavily uses ADO Recordsets.

When using Remoting, it is not possible to send an 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 an ADODB Recordset. There had to be an easier solution. After trial and errors, I found the perfect solution (it could be adapted easily for a Web Service: simply uuEncode and decode the byte array).

Here it is, with just a few lines of code....

The Code

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;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

StephHamel

Web Developer

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 26 Sep 2006
Article Copyright 2006 by StephHamel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid