Search code examples
.netinteropadodbrecordset

ADODB .NET Interop Cursors


I can't figure out why this is happening...

I have the following code:

        var connection = new Connection();
        connection.CursorLocation = CursorLocationEnum.adUseClient;
        connection.ConnectionString = GetOdbcConnectionString(connectionString);
        connection.Open();

        var rs = new Recordset();
        rs.CursorType = CursorTypeEnum.adOpenStatic;
        rs.CursorLocation = CursorLocationEnum.adUseClient;
        rs.LockType = LockTypeEnum.adLockBatchOptimistic;
        // this issues the SQL SELECT * FROM Test
        rs.Open("SELECT * FROM Test", connection); 
        rs.ActiveConnection = null;
        rs.Close();
        rs = null;

        var rs2 = new Recordset();
        rs2.CursorType = CursorTypeEnum.adOpenStatic;
        rs2.CursorLocation = CursorLocationEnum.adUseClient;
        rs2.LockType = LockTypeEnum.adLockBatchOptimistic;

        /* this doesn't output the expected SQL...it outputs:
declare @p1 int
set @p1=180150003
declare @p3 int
set @p3=4
declare @p4 int
set @p4=1
declare @p5 int
set @p5=-1
exec sp_cursoropen @p1 output,N'SELECT * FROM Test',@p3 output,@p4 output,@p5 output
select @p1, @p3, @p4, @p5

*/

        rs2.Open("SELECT * FROM Test", connection);

If I manually Marshal.ReleaseComObject(rs) before opening rs2, the second recordset issues the same simple SQL as the first, without using cursors. Something must be hanging on to it from the first rs....but I don't know what, why or why it's causing the use of cursors, when the connection and recordsets all have OpenStatic and UseClient set.


Solution

  • After banging my head against this for a long time, finally found the problem:

    DRIVER={SQL Server};
    

    needed to change to

    Provider=SQLOLEDB;