Can I share a single database connection between ODBC and ADO?
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 oth开发者_运维百科er?
(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).
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)
精彩评论