views:

173

answers:

1

Is it possible to have an ODBC connection and an ADO connection share the same underlying SQL Server connection, so that both are using the same SPID?

Currently I am using SQLDriverConnect and ADODB::_ConnectionPtr->Open. I can make these connections in either order, so perhaps it's possible to open one making use of the other?

(Language is C++, database is SQL Server 2005 & 2008. ODBC connection string uses a DSN. ADO uses Provider=SQLNCLI10, but either could be changed if required).

A: 

ADO uses OleDB, so you cannot share a connection with ODBC. Even if you use the ADO provider for ODBC you still wont be able, as no ADO API allows to wrap an existing ODBC connection handle.

But the real question is: why do you want to share the connection? The only valid reason is to enroll the two connections in the same transaction without doing a loopback local distributed transaction. Enrolling two distinct connections in the same transaction was always possible, this is how MTS and COM+ worked. The first connection uses sp_getbindtoken to get an enrolment token and the second connection uses sp_bindsession to enroll itself. This mechanism is on the endangered list of deprecation, but after all you are asking about the technology of the 90s... (ODBC, ADO)

Remus Rusanu
The application in question uses the SPID as a locking identifier. We're migrating to ADO from ODBC, and I wanted to be able to convert it piecemeal. Unfortunately this means I'll either have to re-code the locking implementation, or convert all the ODBC code to ADO. (And yes, the app dates from that era).
stusmith
I see. Perhaps you can do the other way around, open the handles in ADO (via the ODBC ADO Provider) then pass the hEnv and hDbc to the ODBC modules to consume.
Remus Rusanu
Thanks all for the comments and answers. We've decided to perform an outright ODBC-ectomy. Just for interest, Remus, how would I go about getting the henv and hdbc from an ADO connection, assuming it had been opened using ADO/ODBC?
stusmith