Question

I have used code like this in the past to return data from a CLR stored procedure:

SqlCommand command = new SqlCommand("select * from accounts", connection)
SqlDataReader r = command.ExecuteReader();
SqlContext.Pipe.Send(r);

I now have to use ODBC to access a Sybase ASE database (so, rather than using SqlCommand I will use OdbcCommand with an OdbcConnection). I can use an OdbcDataReader to get the result set but this cannot be fed into an SqlContext.Pipe.

Is there an Odbc equivalent of SqlContext or can anyone tell me how to return the result set.

Was it helpful?

Solution

To my knowledge, the only way to send results that did not originate from the context connection is to build a new result set metadata object and then send the rows up one at a time.

Something like:

    private static void sendResultsetRecords(SqlDataReader dataReader)
    {
        SqlMetaData[] meta = createMetaDataForResultset(dataReader);

        SqlContext.Pipe.SendResultsStart(new SqlDataRecord(meta));
        sendEachRecordOfData(dataReader, meta);
        SqlContext.Pipe.SendResultsEnd();
    }

You can find the complete implementation in the tSQLt.ResultsetFilter method that is part of the tSQLt database unit testing framework for SQL Server.

The relevant code starts at line 56 of this file: http://sourceforge.net/p/tsqlt/code/HEAD/tree/tSQLtCLR/tSQLtCLR/ResultSetFilter.cs#l56

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top